Java ArrayList removeIf() Method Example

ArrayList Methods with Examples


Java ArrayList Examples


ArrayList removeIf() iterate the list and removes all of the elements of this list that satisfy the given predicate.

Java ArrayList removeIf() Method Example

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class ArrayListDemo {

    public static void main(String[] args) {

        List < String > programmingLanguages = new ArrayList < > ();
        programmingLanguages.add("C");
        programmingLanguages.add("C++");
        programmingLanguages.add("Java");
        programmingLanguages.add("Kotlin");

        // Remove all the elements that satisfy the given predicate
        programmingLanguages.removeIf(new Predicate < String > () {
            @Override
            public boolean test(String s) {
                return s.startsWith("C");
            }
        });

        programmingLanguages.forEach(key - > System.out.println(key));
    }
}
Output:
Java
Kotlin

Reference

Comments