-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredicateExample2.java
More file actions
36 lines (26 loc) · 943 Bytes
/
Copy pathPredicateExample2.java
File metadata and controls
36 lines (26 loc) · 943 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 multiple strings
* and return a new list containing elements that match a given string
*/
public class PredicateExample2 {
public static void main(String[] args) {
List<String> list = Arrays.asList("Hello", "", "YouTube", "", "HelloWorld");
Predicate<String> predicate = s -> s.contains("Hello");
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;
}
}