-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterExample.java
More file actions
46 lines (38 loc) · 1.23 KB
/
Copy pathFilterExample.java
File metadata and controls
46 lines (38 loc) · 1.23 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
package lambda.ex2;
import java.util.ArrayList;
import java.util.List;
public class FilterExample {
public static List<Integer> filter(List<Integer> list, MyPredicate predicate){
List<Integer> result = new ArrayList<>();
for (int val : list) {
if(predicate.test(val)) {
result.add(val);
}
}
return result;
}
public static void main(String[] args) {
List<Integer> numbers = List.of(-3,-2,-1, 2, 3, 5);
System.out.println("원본 리스트 : " + numbers);
MyPredicate negative = new MyPredicate() {
@Override
public boolean test(int value) {
if (value < 0)
return true;
return false;
}
};
MyPredicate even = new MyPredicate() {
@Override
public boolean test(int value) {
if(value % 2 == 0)
return true;
return false;
}
};
// 1, 음수(negative)만 뽑아내기
System.out.println(filter(numbers, negative).toString());
// 2. 짝수(even)만 뽑아내기
System.out.println(filter(numbers, even).toString());
}
}