Java ArrayList removeAll() Method Example

ArrayList Methods with Examples


Java ArrayList Examples


ArrayList removeAll() removes all of the matching elements that are contained in the specified method argument collection. It removes all occurrences of matching elements, not only the first occurrence.

Java ArrayList removeAll() Method Example

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

public class ArrayListDemo {

    public static void main(String[] args) {

        List < String > list = new ArrayList < > ();
        list.add("element 1");
        list.add("element 2");
        list.add("element 3");
        list.add("element 4");

        // Removes from this list all of its elements that are
        // contained in the specified collection (optional operation).
        List < String > list1 = new ArrayList < > ();
        list1.add("element 1");
        list1.add("element 2");
        System.out.println("removeAll operation example ---> " + list.removeAll(list1));

        list.forEach(key - > System.out.println(key));
    }
}
Output:
removeAll operation example ---> true
element 3
element 4

Reference

Comments