The removeIf() method of the ArrayList class removes all elements that satisfy a specified condition (predicate). It provides a concise and efficient way to perform conditional bulk removal without explicitly iterating over the list.
- Introduced in Java 8.
- Uses the Predicate functional interface.
- Supports lambda expressions for concise and readable code.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// create an ArrayList of integers
ArrayList<Integer> num = new ArrayList<>();
// Adding numbers to
// the ArrayList
num.add(23);
num.add(32);
num.add(45);
num.add(63);
// Using removeIf() method to remove
// numbers divisible by 3
num.removeIf(n -> (n % 3 == 0));
System.out.println(num);
}
}
Output
[23, 32]
Explanation: In the above example, the removeIf() method evaluates each element using the predicate n -> (n % 3 == 0). Elements divisible by 3 (45 and 63) satisfy the condition and are removed, while the remaining elements stay in the ArrayList.
Syntax
public boolean removeIf(Predicate<? super E> filter)
- Parameter: This method takes a parameter "filter" that specifies the condition for removing elements.
- Return Type: This method returns true if any elements were removed; otherwise, false.
- Exception: This method throws NullPointerException if the specified filter is null.
Examples of Java ArrayList removeIf() Method
Example: Removing Strings that Start with a Specific Character
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Creating an ArrayList of student names
ArrayList<String> s = new ArrayList<>();
// Adding student names to the ArrayList
s.add("Sweta");
s.add("Gudly");
s.add("Sohan");
s.add("Amiya");
s.add("Ram");
// Using removeIf() method to
// remove names starting with 'S'
s.removeIf(name -> name.startsWith("S"));
System.out.println("Students whose names do not start with S:");
System.out.println(s);
}
}
Output
Students whose names do not start with S: [Gudly, Amiya, Ram]
Explanation: Here, the predicate name -> name.startsWith("S") checks whether each string begins with the character 'S'. All matching names are removed, and only the remaining names are displayed.
Example: Removing Empty Strings
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("");
list.add("Python");
list.add("");
list.add("C++");
list.removeIf(String::isEmpty);
System.out.println(list);
}
}
Output
[Java, Python, C++]
Explanation: The method reference String::isEmpty removes every empty string from the ArrayList, leaving only the non-empty elements.
Difference Between remove() and removeIf()
remove() | removeIf() |
|---|---|
| Removes an element by value or index. | Removes elements that satisfy a condition. |
| Removes one element at a time. | Removes multiple matching elements in one call. |
| Accepts an object or index. | Accepts a Predicate. |
| Available before Java 8. | Introduced in Java 8. |
| Does not use lambda expressions. | Supports lambda expressions and method references. |
Advantages of removeIf()
- Removes multiple matching elements in a single method call.
- Eliminates the need for explicit loops.
- Supports lambda expressions and method references.
- Makes conditional removal concise and readable.
- Reduces boilerplate code.
- Improves code maintainability for filtering operations.