forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilteringApples.java
More file actions
92 lines (75 loc) · 2.43 KB
/
Copy pathFilteringApples.java
File metadata and controls
92 lines (75 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.brianway.learning.java8.lambda;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* Created by brian on 16/12/26.
*
* 传递代码的例子
* 分别使用了方法引用和Lambda(匿名函数)
*/
public class FilteringApples {
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(new Apple(80, "green"),
new Apple(155, "green"),
new Apple(120, "red"));
List<Apple> greenApples = filter(inventory, FilteringApples::isGreenApple);
System.out.println(greenApples);
List<Apple> greenApples2 = filter(inventory, (Apple a) -> "green".equals(a.getColor()));
System.out.println(greenApples2);
List<Apple> heavyApples = filter(inventory, FilteringApples::isHeavyApple);
System.out.println(heavyApples);
List<Apple> heavyApples2 = filter(inventory, (Apple a) -> a.getWeight() > 150);
System.out.println(heavyApples2);
}
/**
* 根据抽象条件筛选
* 将迭代集合的逻辑和要应用到集合中每个元素的行为区分开
*
* @param inventory
* @param p
* @return
*/
public static List<Apple> filter(List<Apple> inventory, Predicate<Apple> p) {
List<Apple> result = new ArrayList<>();
for (Apple apple : inventory) {
if (p.test(apple)) {
result.add(apple);
}
}
return result;
}
public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
}
public static boolean isHeavyApple(Apple apple) {
return apple.getWeight() > 150;
}
public static class Apple {
private int weight = 0;
private String color = "";
public Apple(int weight, String color) {
this.weight = weight;
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}';
}
}
}