What is ArrayList.removeIf() method in Java?
Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.
The removeIf method will loop through all the elements of the list and remove the elements that match the condition of the predicate Predicate is a functional interface, which takes one argument and returns either true or false based on the condition defined. function, which is sent as the argument to the removeIf method.
Syntax
public boolean removeIf(Predicate filter);
Example
import java.util.ArrayList;class RemoveIfpublic static void main( String args[] )ArrayList numbers = new ArrayList<>();numbers.add(1);numbers.add(-10);numbers.add(30);numbers.add(-3);System.out.println("The numbers list is " + numbers);numbers.removeIf(number -> number < 0);System.out.println("After removing negative elements, the numbers list is => " + numbers);>>In the code above, we created a numbers ArrayList and added some positive and negative numbers to it.
Then, we passed a predicate Predicate is a functional interface, which takes one argument and returns either true or false based on the condition defined. function to call the removeIf method. The predicate function will be tested against each element of the numbers list.
Example with a list of objects
import java.util.ArrayList;class RemoveIdpublic static void main( String args[] )ArrayList users = new ArrayList<>();users.add(new User("Ram", 15));users.add(new User("Ralph", 13));users.add(new User("Raj", 25));users.add(new User("Rahul", 24));System.out.println("The users list is");System.out.println(users);users.removeIf( user -> ! user.canVote() );System.out.println("\nAfter removing users who cannot vote, the users list is");System.out.println(users);>>In the code above, we created a users ArrayList and added some objects of the User class.
Then, we passed a predicate Predicate is a functional interface, which takes one argument and returns either true or false based on the condition defined. function to call the removeIf method on the users list. The predicate function will be tested against each element of the users list.
In the predicate function, we checked if the object does not satisfy the canVote condition defined in the User class. If the predicate function returns true , the current object will be removed from the users list.
Java ArrayList.removeIf()
The ArrayList.removeIf() iterates the list and removes all of the elements of this ArrayList that satisfy the given Predicate.
The removeIf() takes a single argument of type Predicate. The Predicate interface is a functional interface that represents a condition (boolean-valued function) of one argument. It checks that is a given argument met the condition or not.
public boolean removeIf(Predicate filter);
Method parameter – filter predicate that returns true for elements to be removed.
Method returns – true if any elements were removed from this list.
Method throws – NullPointerException if predicate is null .2. ArrayList.removeIf() Example
The following Java programs use removeIf() method to remove elements that match a Predicate.
2.1. Remove All Even Numbers
In this simple example, we have a list of odd/even numbers and remove all even numbers from the list.
ArrayList numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); numbers.removeIf(number -> number % 2 == 0); System.out.println(numbers);
2.2. Remove Objects by Field
In this simple example, we have a list of employees, and we are removing all employees whose names start with char ‘P’ . The predicate, employee.getName().startsWith(“P”) matches the name of each Employee and returns true if the name starts with P.
ArrayList employees = new ArrayList<>(); employees.add(new Employee(1l, "Alex", LocalDate.of(2018, Month.APRIL, 21))); employees.add(new Employee(4l, "Brian", LocalDate.of(2018, Month.APRIL, 22))); employees.add(new Employee(3l, "Piyush", LocalDate.of(2018, Month.APRIL, 25))); employees.add(new Employee(5l, "Charles", LocalDate.of(2018, Month.APRIL, 23))); employees.add(new Employee(2l, "Pawan", LocalDate.of(2018, Month.APRIL, 24))); Predicate condition = employee -> employee.getName().startsWith("P"); employees.removeIf(condition); System.out.println(employees);
[ Employee [id=1, name=Alex, dob=2018-04-21], Employee [id=4, name=Brian, dob=2018-04-22], Employee [id=5, name=Charles, dob=2018-04-23] ]
That’s all for the ArrayList removeIf() in Java.
Java ArrayList removeIf() Method
The Java ArrayList removeIf(Predicate filter) method retrieves and removes all the elements of this ArrayList that satisfy the given predicate. In case of exception, the exception is relayed to the caller.
Declaration
Following is the declaration for java.util.ArrayList.removeIf(filter) method
public boolean removeIf(Predicate filter)Parameters
Return Value
true if any elements were removed.
Exception
NullPointerException − if the specified filter is null
Example 1
The following example shows the usage of Java ArrayList removeIf(filter) method. We’re creating an ArrayList of Integers, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo < public static void main(String[] args) < // create an empty array list ArrayListarrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(1); arrayList.add(2); arrayList.add(3); arrayList.add(4); arrayList.add(5); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove even numbers from the arrayList arrayList.removeIf(i -> i%2 == 0); // let us print all the elements available in arrayList again System.out.println("ArrayList result notranslate"> ArrayList = [1, 2, 3, 4, 5] ArrayList = [1, 3, 5]Example 2
The following example shows the usage of Java ArrayList removeIf(filter) method. We’re creating an ArrayList of String, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo < public static void main(String[] args) < // create an empty array list ArrayListarrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add("A"); arrayList.add("BB"); arrayList.add("C"); arrayList.add("DD"); arrayList.add("E"); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove single length string arrayList.removeIf(i -> i.length() == 1); // let us print all the elements available in arrayList again System.out.println("ArrayList result notranslate"> ArrayList = [A, BB, C, DD, E] ArrayList = [BB, DD]Example 3
The following example shows the usage of Java ArrayList removeIf(filter) method. We’re creating an ArrayList of Student objects, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As ArrayList is modified it is printed to check if even numbers are removed or not.
package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo < public static void main(String[] args) < // create an empty array list ArrayListarrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(new Student(1, "Julie")); arrayList.add(new Student(2, "Robert")); arrayList.add(new Student(3, "Adam")); arrayList.add(new Student(4, "Jene")); arrayList.add(new Student(5, "Jacob")); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove if roll no is 2 arrayList .removeIf(i -> i.rollNo == 2); // let us print all the elements available in arrayList again System.out.println("ArrayList = " + arrayList); > > class Student < int rollNo; String name; Student(int rollNo, String name)< this.rollNo = rollNo; this.name = name; >@Override public String toString() < return "[ " + this.rollNo + ", " + this.name + " ]"; >@Override public boolean equals(Object obj) < Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); >>Output
Let us compile and run the above program, this will produce the following result −
ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]] ArrayList = [[ 1, Julie ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]