The removeIf() method of ArrayList is used to remove all of the elements of this ArrayList that satisfies a given predicate filter which is passed as a parameter to the method.
Errors or runtime exceptions are thrown during iteration or by the predicate are pass to the caller. This method returns True, if we are able to remove some element.
Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition checking function, check the given input for a given condition and returns a boolean result for the same indicating whether the condition was met or not.
public boolean removeIf(Predicate filter)
Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed.
Returns: This method returns True if predicate returns true and we are able to remove elements.
Exception: This method throws NullPointerException if the specified filter is null.
Below programs illustrate removeIf() method of ArrayList:
Program 1: Program to demonstrate removeIf() method on ArrayList which contains a set of Numbers and only the numbers which are divisible by 3 will be removed.
Program 2: Program to demonstrate removeIf() method on ArrayList which contains set of Students Names and names starting with “S” will be removed.
Students name Does not start with S Ram Mohan Rabi
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Vector removeIf() method in Java
- CopyOnWriteArraySet removeIf() method in Java with Examples
- LinkedTransferQueue removeIf() method in Java with Examples
- CopyOnWriteArrayList removeIf() method in Java with Examples
- LinkedBlockingDeque removeIf() method in Java with Examples
- ArrayList of ArrayList in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.Arraylist.indexOf() in Java
- ArrayList toArray() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList retainAll() method in Java
- ArrayList spliterator() method in Java
- ArrayList forEach() method in Java
- ArrayList iterator() method in Java with Examples
- ArrayList ensureCapacity() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList listIterator() method in Java with Examples
- ArrayList set() 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 email@example.com. 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.