-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterExample2.java
More file actions
33 lines (25 loc) · 945 Bytes
/
Copy pathFilterExample2.java
File metadata and controls
33 lines (25 loc) · 945 Bytes
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
package lambda.ex3;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class FilterExample2 {
public static List<Integer> filter(List<Integer> list, Predicate 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);
Predicate<Integer> negative = (value) -> value < 0;
Predicate<Integer> even = (value) -> value % 2 == 0;
// 1, 음수(negative)만 뽑아내기
System.out.println(filter(numbers, negative).toString());
// 2. 짝수(even)만 뽑아내기
System.out.println(filter(numbers, even).toString());
}
}