-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateExample1.java
More file actions
36 lines (26 loc) · 901 Bytes
/
Copy pathPredicateExample1.java
File metadata and controls
36 lines (26 loc) · 901 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 the take a list with empty strings
* and return a new list without empty strings
*/
public class PredicateExample1 {
public static void main(String[] args) {
List<String> list = Arrays.asList("The", "", "Walking", "", "Dead");
Predicate<String> predicate = s -> !s.isEmpty();
List<String> newList = filterList(list, predicate);
System.out.println("list - " + list);
System.out.println("newList - " + newList);
}
private static List<String> filterList(List<String> list, Predicate<String> predicate) {
List<String> newList = new ArrayList<>();
for (String string : list) {
if (predicate.test(string))
newList.add(string);
}
return newList;
}
}