In Java, elements can be removed from a List based on a specific condition using a Predicate, which represents a true/false test. Java provides multiple efficient ways to do this, such as using an Iterator, removeAll(), Streams (Java 8), and removeIf(). These approaches help safely modify lists, avoid runtime errors, and write clean, readable code.
Method 1: Using Iterator
This approach safely removes elements while iterating over the list and avoids ConcurrentModificationException.
Example: This program shows how to remove null elements from a Java List using an Iterator and a Predicate.
import java.util.*;
import java.util.function.Predicate;
class GFG {
public static <T> List<T> removeNullUsingIterator(List<T> l, Predicate<T> p) {
Iterator<T> itr = l.iterator();
while (itr.hasNext()) {
T t = itr.next();
if (!p.test(t)) {
itr.remove();
}
}
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> isNotNull = Objects::nonNull;
l = removeNullUsingIterator(l, isNotNull);
System.out.println("List with null values removed: " + l);
}
}
Output
List with null values: [Geeks, null, forGeeks, null, A computer portal] List with null values removed: [Geeks, forGeeks, A computer portal]
Explanation:
- The code removes null values from a List using an Iterator and a Predicate.
- removeNullUsingIterator() iterates through the list safely using Iterator.
- Objects::nonNull is used as a predicate to keep only non-null elements.
- If an element does not satisfy the predicate, it is removed using itr.remove().
Method 2: Using List.removeAll()
In this method, elements satisfying the predicate are first collected and then removed using removeAll()
Example: This program shows how to remove specific elements from a List in Java using a Predicate and the removeAll() method.
import java.util.*;
import java.util.function.Predicate;
class GFG {
public static <T> List<T> removeElements(List<T> l, Predicate<T> p) {
Collection<T> collection = new ArrayList<>();
for (T t : l) {
if (p.test(t)) {
collection.add(t);
}
}
l.removeAll(collection);
return l;
}
public static void main(String[] args) {
List<String> l = new ArrayList<>(
Arrays.asList("1", "10", "15", "10", "12", "5", "10", "20"));
System.out.println("Original List: " + l);
Predicate<String> is10 = i -> i.equals("10");
l = removeElements(l, is10);
System.out.println("Updated List: " + l);
}
}
Output
Original List: [1, 10, 15, 10, 12, 5, 10, 20] Updated List: [1, 15, 12, 5, 20]
Explanation :
- removeElements() takes a List and a Predicate as input.
- It creates a temporary collection to store elements that match the condition.
- The for loop checks each element using p.test(t).
- All matching elements (here, "10") are added to the temporary collection.
- l.removeAll(collection) removes those elements from the original list.
- In main(), the predicate i -> i.equals("10") removes all occurrences of "10" from the list.
Method 3: Using Java 8 Streams (Lambda Expressions)
Java 8 Streams provide a clean and functional way to filter elements.
Example: This Java program uses Java 8 Streams and a Predicate to efficiently remove unwanted elements, like nulls, from a list.
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
class GFG {
public static <T> List<T> removeElements(List<T> l, Predicate<T> p) {
return l.stream()
.filter(p)
.collect(Collectors.toList());
}
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> isNotNull = i -> i != null;
l = removeElements(l, isNotNull);
System.out.println("List with null values removed: " + l);
}
}
Output
List with null values: [Geeks, null, forGeeks, null, A computer portal] List with null values removed: [Geeks, forGeeks, A computer portal]
Explanation:
- A Predicate<String> (isNotNull) checks if an element is not null.
- The removeElements() method uses Java 8 Streams:
- .filter(p) keeps only elements that satisfy the predicate.
- .collect(Collectors.toList()) collects the filtered elements into a new list.
Method 4: Using removeIf()
removeIf() directly removes all elements that satisfy the given predicate.
Example:This program shows how to remove elements from a list that meet a specific condition using Java’s removeIf() method with a Predicate.
import java.util.*;
import java.util.function.Predicate;
class GFG {
public static <T> List<T> removeElements(List<T> l, Predicate<T> p) {
l.removeIf(p);
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);
}
}
Output
List with null values: [Geeks, null, forGeeks, null, A computer portal] List with null values removed: [Geeks, forGeeks, A computer portal]
Explanation:
- A Predicate is defined to identify null elements.
- removeIf() removes all elements that satisfy the Predicate.