-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateExample3.java
More file actions
36 lines (26 loc) · 874 Bytes
/
Copy pathPredicateExample3.java
File metadata and controls
36 lines (26 loc) · 874 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
34
35
36
package com.codecafe.java8.functionalinterfaces.predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/*
* Goal is to make the filterList method generic
* so that the filterList method can work with any datatype
*/
public class PredicateExample3 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 3, 4, 5, 4, 5, 5, 5, 6);
Predicate<Integer> evenFilter = e -> e % 2 == 0;
List<Integer> newList = filterList(list, evenFilter);
System.out.println("list - " + list);
System.out.println("newList - " + newList);
}
private static <T> List<T> filterList(List<T> list, Predicate<T> predicate) {
List<T> newList = new ArrayList<>();
for (T t : list) {
if (predicate.test(t))
newList.add(t);
}
return newList;
}
}