Below are the methods to efficiently remove elements from a List satisfying a Predicate condition:
p ==> Predicate, specifying the condition
l ==> List, from which element to be removed
Using iterator
Below program demonstrates the removal of null elements from the list, using the Predicate
Java
import java.util.function.Predicate;
import java.util.*;
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> isNull = item -> Objects.nonNull(item);
l = removeNullUsingIterator(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]
Time complexity : O(N^2)
Space complexity: O(N)
Using List.removeAll()
In this method, a collection containing elements to be removed is used to remove those elements from the original l. It requires creating a collection with the required elements using a Predicate condition. After doing this, the original l is searched for this collection and all instances of it are removed.
Java
import java.util.function.Predicate;
import java.util.*;
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);
}
}
System.out.println( "Collection to be removed: " + collection);
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 == "10" );
l = removeElements(l, is10);
System.out.println( "Updated List: " + l);
}
}
|
Output
Original List: [1, 10, 15, 10, 12, 5, 10, 20]
Collection to be removed: [10, 10, 10]
Updated List: [1, 15, 12, 5, 20]
Using Lambdas (Java 8)
Stream.filter() method can be used in Java 8 that returns a stream consisting of the elements
that match the given predicate condition.
Java
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.*;
class GFG {
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 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]
Using removeIf()
As the name suggests, it is a direct method to remove all elements that satisfy the given predicate.
Java
import java.util.function.Predicate;
import java.util.*;
class GFG {
public static <T> List<T>
removeElements(List<T> l, Predicate<T> p)
{
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);
}
}
|
Output
List with null values: [Geeks, null, forGeeks, null, A computer portal]
List with null values removed: [Geeks, forGeeks, A computer portal]
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
23 Jul, 2022
Like Article
Save Article