Remove elements from a List that satisfy given predicate in Java
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 removal of null elements from list, using the Predicate
// Java Program to remove nulls// from a List using iterator and Predicateimportjava.util.function.Predicate;importjava.util.*;classGFG {// Generic function to remove Null Using Iteratorpublicstatic<T> List<T>removeNullUsingIterator(List<T> l, Predicate<T> p){// Create an iterator from the lIterator<T> itr = l.iterator();// Find and remove all nullwhile(itr.hasNext()) {// Fetching the next elementT t = itr.next();// Checking for Predicate conditionif(p.test(t)) {// If the condition matches,// remove that elementitr.remove();}}// Return the nullreturnl;}publicstaticvoidmain(String[] args){// Create the l with null valuesList<String> l =newArrayList<>(Arrays.asList("Geeks",null,"forGeeks",null,"A computer portal"));// Print the listSystem.out.println("List with null values: "+ l);// Creating a Predicate condition checking for nullPredicate<String> isNull = item -> Objects.nonNull(item);// Removing nulls using iterator and Predicatel = removeNullUsingIterator(l, isNull);// Print the listSystem.out.println("List with null values removed: "+ l);}}chevron_rightfilter_noneOutput:
List with null values: [Geeks, null, forGeeks, null, A computer portal] List with null values removed: [Geeks, forGeeks, A computer portal]
-
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 Predicate condition. After doing this, the original l is searched for this collection and all instances of it is removed.
// Java Program to remove 10// from a List using List.removeAll() and Predicateimportjava.util.function.Predicate;importjava.util.*;classGFG {// Generic function to remove elements using Predicatepublicstatic<T> List<T>removeElements(List<T> l, Predicate<T> p){// Create collection using PredicateCollection<T> collection =newArrayList<>();for(T t : l) {if(p.test(t)) {collection.add(t);}}// Print the listSystem.out.println("Collection to be removed: "+ collection);// Removing 10 using List.removeAll()// passing a collectionl.removeAll(collection);// Return the listreturnl;}publicstaticvoidmain(String[] args){// Create a list with null valuesList<String> l =newArrayList<>(Arrays.asList("1","10","15","10","12","5","10","20"));// Print the listSystem.out.println("Original List: "+ l);// Creating a Predicate condition checking for 10Predicate<String> is10 = i -> (i =="10");// Removing using Predicatel = removeElements(l, is10);// Print the listSystem.out.println("Updated List: "+ l);}}chevron_rightfilter_noneOutput:
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 Program to remove nulls// from a List using Java 8importjava.util.function.Predicate;importjava.util.stream.Collectors;importjava.util.*;classGFG {// Generic function to remove elements using Predicatepublicstatic<T> List<T>removeElements(List<T> l, Predicate<T> p){// Removing nulls using Java Stream// using Predicate condition in lambda expressionl = l.stream().filter(p).collect(Collectors.toList());// Return the listreturnl;}publicstaticvoidmain(String[] args){// Create a list with null valuesList<String> l =newArrayList<>(Arrays.asList("Geeks",null,"forGeeks",null,"A computer portal"));// Print the listSystem.out.println("List with null values: "+ l);// Creating a Predicate condition checking for nullPredicate<String> isNull = i -> (i ==null);// Removing using Predicatel = removeElements(l, isNull);// Print the listSystem.out.println("List with null values removed: "+ l);}}chevron_rightfilter_noneOutput:
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 given predicate.
// Java Program to remove nulls// from a List using Java 8importjava.util.function.Predicate;importjava.util.*;classGFG {// Generic function to remove elements using Predicatepublicstatic<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 the listreturnl;}publicstaticvoidmain(String[] args){// Create a list with null valuesList<String> l =newArrayList<>(Arrays.asList("Geeks",null,"forGeeks",null,"A computer portal"));// Print the listSystem.out.println("List with null values: "+ l);// Creating a Predicate condition checking for nullPredicate<String> isNull = i -> (i ==null);// Removing using Predicatel = removeElements(l, isNull);// Print the listSystem.out.println("List with null values removed: "+ l);}}chevron_rightfilter_noneOutput:
List with null values: [Geeks, null, forGeeks, null, A computer portal] List with null values removed: [Geeks, forGeeks, A computer portal]
Recommended Posts:
- Java 8 Predicate with Examples
- How to remove a SubList from a List in Java
- Remove all elements from the ArrayList in Java
- Remove repeated elements from ArrayList in Java
- Java program to remove nulls from a List Container
- List remove(int index) method in Java with Examples
- List remove(Object obj) method in Java with Examples
- Stream iterate(T,Predicate,UnaryOperator) method in Java with examples
- Count occurrences of elements of list in Java
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- PriorityQueue remove() Method in Java
- Stack remove(int) method in Java with Example
- ConcurrentLinkedQueue remove() method in Java
- Set remove() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



