码迷,mamicode.com
首页 > 编程语言 > 详细

java面向函数编程简单应用

时间:2019-09-11 21:54:08      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:func   stream   function   函数式   port   style   equals   main   ogg   

 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面向函数编程简单应用

标签:func   stream   function   函数式   port   style   equals   main   ogg   

原文地址:https://www.cnblogs.com/lovemeng1314/p/11508765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!