forked from learning-zone/java-basics
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathListUsingRemoveAll.java
More file actions
55 lines (41 loc) · 1.39 KB
/
ListUsingRemoveAll.java
File metadata and controls
55 lines (41 loc) · 1.39 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package strings;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Predicate;
public class ListUsingRemoveAll {
// Generic function to remove elements using Predicate
public static <T> List<T>
removeElements(List<T> l, Predicate<T> p)
{
// Create collection using Predicate
Collection<T> collection = new ArrayList<>();
for (T t : l) {
if (p.test(t)) {
collection.add(t);
}
}
// Print the list
System.out.println("Collection to be removed: " + collection);
// Removing 10 using List.removeAll()
// passing a collection
l.removeAll(collection);
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List<String> l = new ArrayList<>(
Arrays.asList("1", "10", "15", "10", "12", "5", "10", "20"));
// Print the list
System.out.println("Original List: " + l);
// Creating a Predicate condition checking for 10
Predicate<String> is10 = i -> (i == "10");
// Removing using Predicate
l = removeElements(l, is10);
// Print the list
System.out.println("Updated List: " + l);
}
}