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 }
版权声明:本文为cheng5350原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。