Java ArrayListremoveIf() Method
Example
Remove all even numbers from a list:
import java.util.ArrayList;public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(6); numbers.add(1); numbers.removeIf( n -> n % 2 == 0 ); System.out.println(numbers); }}Definition and Usage
TheremoveIf() method removes all elements from this list for which a condition is satisfied. The condition can be defined by the return value of a lambda expression that is compatible with thetest() method of Java'sPredicate interface.
To learn about lambda expressions, see ourJava Lambda Expression tutorial.
Syntax
public boolean removeIf(Predicatecondition)Parameter Values
| Parameter | Description |
|---|---|
| condition | Required. APredicate object or lambda expression which tests an item from the list. |
Technical Details
| Returns: | true if any items were removed from the list,false otherwise. |
|---|
Related Pages
❮ ArrayList Methods

