Java ArrayList removeIf() Example

The removeIf() method removes all of the elements of a collection that satisfy the given predicate.

Java ArrayList removeIf() Example

import java.util.ArrayList;
import java.util.List;

public class RemoveIfEx {

    public static void main(String[] args) {
        
        List<Integer> values = new ArrayList<>();
        values.add(5);
        values.add(-3);
        values.add(2);
        values.add(8);
        values.add(-2);
        values.add(6);

        values.removeIf(val -> val < 0);
        
        System.out.println(values);
    }
}

Output

[5, 2, 8, 6]

Java ArrayList Source Code Examples


Comments