java面向函数编程简单应用
Posted lovemeng1314
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java面向函数编程简单应用相关的知识,希望对你有一定的参考价值。
1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.List; 4 import java.util.function.Predicate; 5 import java.util.logging.Filter; 6 7 public class 函数式接口 8 9 public static void main(String[] args) 10 List<Apple> apples = Arrays.asList(new Apple("red", 10), 11 new Apple("green", 20), new Apple("red", 16)); 12 List<Apple> apples1 = filterApple(apples, apple -> (apple.color).equals("red") && apple.size>10); 13 apples1.stream().forEach(System.out::print); 14 15 public static List<Apple> filterApple(List<Apple> list,Predicate<Apple> predicate) 16 List<Apple> list1 = new ArrayList<>(); 17 for (Apple apple : list) 18 if(predicate.test(apple)) 19 list1.add(apple); 20 21 22 return list1; 23 24 25 26 class Apple 27 String color; 28 int size; 29 30 @Override 31 public String toString() 32 return "Apple" + 33 "color=‘" + color + ‘\‘‘ + 34 ", size=" + size + 35 ‘‘; 36 37 38 public Apple(String color, int size) 39 this.color = color; 40 this.size = size; 41 42 43 public String getColor() 44 return color; 45 46 47 public void setColor(String color) 48 this.color = color; 49 50 51 public int getSize() 52 return size; 53 54 55 public void setSize(int size) 56 this.size = size; 57 58
以上是关于java面向函数编程简单应用的主要内容,如果未能解决你的问题,请参考以下文章
简单的 Node/Express 应用程序,函数式编程方式(如何在 JavaScript 中处理副作用?)