The removeIf()
method is a powerful method in the Java ArrayList
class that lets you remove all elements that satisfy a specified condition—provided as a functional predicate or lambda expression. This approach is especially useful when you want to filter out multiple elements at once based on a dynamic rule, all in a single, concise statement.
What Is the removeIf() Method in ArrayList?
- The removeIf() method removes every element from the list for which the given condition (predicate) returns
true
. - The condition is specified via a lambda expression or any implementation of the
Predicate
functional interface. - The method operates in-place, modifying the original
ArrayList
directly. - Returns
true
if any elements were removed; otherwise, returnsfalse
.
Syntax
public boolean removeIf(Predicate<? super E> filter)
Where:
filter
is a predicate (lambda or method reference) that returnstrue
for elements to be removed.E
is the type of elements in the list.
Parameters
Parameter | Description |
---|---|
filter | A Predicate (condition) that returns true for items to remove |
Return Value
Returns | Description |
---|---|
true | If one or more elements were removed |
false | If no elements were removed |
Exceptions
- Throws
NullPointerException
if the specified predicate isnull
. - Any runtime exception thrown by the predicate during iteration will be propagated to the caller.
How Does removeIf() Work Internally?
- Iterates over all elements in the list.
- Evaluates each element with the given predicate.
- Removes elements for which the predicate returns
true
. - Shifts subsequent elements to fill removed positions, then updates the size of the list.
Examples of the removeIf() Method
1. Remove All Even Numbers from an ArrayList
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); numbers.add(6); // Remove all even numbers numbers.removeIf(n -> n % 2 == 0); System.out.println(numbers); // Output: [1, 3, 5] } }
In this example, every even number is removed using a lambda expression as a predicate.
2. Remove Strings Containing a Specific Substring
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> countries = new ArrayList<>(); countries.add("Iceland"); countries.add("America"); countries.add("Ireland"); countries.add("Canada"); countries.add("Greenland"); // Remove all countries containing "land" countries.removeIf(str -> str.contains("land")); System.out.println(countries); // Output: [America, Canada] } }
Removes every country whose name contains “land”, leaving only others.
3. Remove All Elements Divisible by 3
import java.util.ArrayList; public class Example { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(23); numbers.add(32); numbers.add(45); numbers.add(63); // Remove numbers that are divisible by 3 numbers.removeIf(n -> n % 3 == 0); System.out.println(numbers); // Output: [23, 32] } }
Filters out all elements for which the condition is true (divisible by 3).
4. Remove Objects Based on Custom Field
import java.util.ArrayList; class Employee { String name; Employee(String name) { this.name = name; } public String toString() { return name; } } public class Demo { public static void main(String[] args) { ArrayList<Employee> employees = new ArrayList<>(); employees.add(new Employee("Alex")); employees.add(new Employee("Brian")); employees.add(new Employee("Pranav")); employees.add(new Employee("Pradeep")); // Remove employees whose names start with 'P' employees.removeIf(emp -> emp.name.startsWith("P")); System.out.println(employees); // Output: [Alex, Brian] } }
Removes all employees whose names start with the letter ‘P’.
Important Notes
- Efficient bulk removal: Removes many elements matching a predicate in one call—no need for manual loop and removal.
- Lambda expressions recommended: Clean and concise with Java 8+ features.
- Predicate flexibility: You can provide any complex logic as your removal condition.
- Null predicate: Throws
NullPointerException
if the predicate is null. - Original list is modified: All matching elements are removed from the source list.
- Works for all data types: You can use it with lists of integers, strings, custom objects, etc.
- Order is preserved: Non-matching elements remain, with their order retained.
Summary
Aspect | Details |
---|---|
Method Purpose | Remove every element matching a condition |
Syntax | list.removeIf(Predicate<? super E> filter) |
Return Value | true if any item was removed, false otherwise |
Predicate | Condition as a lambda or predicate |
Exceptions | NullPointerException if predicate is null |
Typical Use Cases | Filtering lists, batch deletion, advanced cleanup |
Typical Use Cases
- Filtering out unwanted data: Remove all entries that meet a specific criterion quickly.
- Batch cleanup: Delete items based on validation rules or other business logic.
- Dynamic data preparation: Prepare lists for output or processing by removing irrelevant entries.