Remove Elements from a List that satisfies given predicate in Java

In this post, we will see how to remove elements from a list (mutable) that satisfies the given predicate.

 
Fail fast iterator vs fail safe iterator in Java

 
It is not allowed to modify a list (add elements to it, remove elements from it, etc) while iterating over it else a ConcurrentModificationException will be thrown to avoid non-deterministic behavior. We can use any of below methods to efficiently remove elements from the list that matches the given predicate, without throwing ConcurrentModificationException.

 

1. Using an Iterator

The Iterator interface provides remove() method which removes the last element returned by the iterator. The idea is to get an iterator to the given list and use remove() method to remove elements on which the predicate is satisfied.

Please note that the iterator will throw a ConcurrentModificationException, if the list is modified after the iterator is created except through the iterator’s own remove method.

 

2. Using removeAll()

We can maintain a separate collection containing elements from the original list that matches the given predicate. Then we can use removeAll() method provided by the List interface to remove elements of that collection from the original list.

 

3. Java 8

In Java 8, we can use Stream API to easily remove elements from a list by filtering the Stream.

 

3.1. Using Collectors:

The idea is to convert the specified list to a sequential Stream, filter the stream and accumulate the elements that match the given predicate into a new List using a Collector.

 
Please note that this method will return a new List instance and the original list remains unchanged. Also it doesn’t guarantee on the type of the List returned, so we can use Collectors.toCollection() to specify the desired List type –

 

3.2. Using forEach() + List.add()

This is similar to approach discussed earlier where we maintain a separate collection which contains the elements from the original list that matches the given predicate and using List.removeAll() method to remove those elements from the original list.

 
We can also perform filtering inside forEach() method as shown below –

 

3.3. Using forEach() + List.remove()

Since we can’t modify a List while iterating over it, we can create a duplicate list and remove elements that satisfies the predicate from the original list by iterating over the duplicate list.

Below code uses Java 8 Stream to do the filtering, but we can also use an Iterator or a for-each loop.

 
We can also perform filtering inside forEach() method as shown below –

 

3.4. Using removeIf()

The List interface provides removeIf() method that removes all elements in the list that satisfy the given predicate. This is the simplest approach among all approaches discussed above and uses Iterator.remove() behind the scenes.

 
 
Suggested Read: Remove Elements from a List within a Loop

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz