Java ArrayList.removeIf() method removes all elements that satisfy a condition by iterating through the elements of the current arraylist and matching them against the condition specified by the argument Predicate.
ArrayList<String> arraylist = new ArrayList<>(Arrays.asList("A", "B", "C", "C", "D")); arraylist.removeIf(e -> e.equals("C")); //[A, B, D]
Note that the ArrayList class also provides other methods for removing elements such as:
- remove(): it removes a single element by value or index position.
- removeAll(): it removes all occurrences of specified elements by their value.
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<? super E> filter);
- Method parameter – filter predicate that returns
truefor elements to be removed.
- Method returns –
trueif any elements were removed from this list.
- Method throws –
NullPointerExceptionif predicate is
2. Examples to Remove Elements Matching a Condition
For removing the elements from the arraylist, we can create the conditions in multiple ways using the Predicate instances. Let us see a few usecases.
2.1. Remove All Even Numbers from a List of Numbers
In this simple example, we have a list of odd and even numbers. Then we use the removeIf() method to remove all even numbers from the list.
ArrayList<Integer> 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);
[1, 3, 5, 7, 9]
2.2. Remove All Objects for Matching Field Value
In another example, we have a list of employees. We are removing all employees whose names start with the character
The predicate, employee.getName().startsWith(“P”) matches the name of each Employee and returns true if the name starts with P.
ArrayList<Employee> 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<Employee> 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] ]
The removeIf() method is a very convenient way to remove multiple elements from the arraylist that match a certain condition. Combined with the power of predicates and functional programming, this method helps in creating concise and readable code for such usecases.
That’s all for the ArrayList removeIf() in Java.
Happy Learning !!