forked from mertsaner/java-interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListUsingLambdas.java
More file actions
39 lines (28 loc) · 811 Bytes
/
ListUsingLambdas.java
File metadata and controls
39 lines (28 loc) · 811 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
37
38
39
package strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ListUsingLambdas {
public static <T> List<T>
removeElements(List<T> l, Predicate<T> p) {
l = l.stream()
.filter(p)
.collect(Collectors.toList());
return l;
}
public static void main(String[] args) {
List<String> l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A counputer portal"));
System.out.println("List with null values: "+l);
// Creating a Predicate condition checking for null
Predicate<String> isNull = i -> (i == null);
l = removeElements(l, isNull);
System.out.println("List with null values removed: " + l);
}
}