forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListRemoveIf.java
37 lines (26 loc) · 761 Bytes
/
ListRemoveIf.java
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
package strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class ListRemoveIf {
public static <T>List<T>
removeElements(List<T> l, Predicate<T> p) {
// Removing nulls using Java Stream
// using Predicate condition in removeIf()
l.removeIf(x -> p.test(x));
return l;
}
public static void main(String[] args) {
List<String> l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
System.out.println("List with null values: "+l);
Predicate<String> isNull = i -> (i == null);
l = removeElements(l, isNull);
System.out.println("List with null values removed: " + l );
}
}