ArrayList removeAll Method Tutorial

boolean removeAll(Collection<?> c)

The removeAll method removes all matching elements in the current ArrayList instance that match all elements from the derived Collection list argument. The Collection portion of the parameter list means that we can pass in any object that is a subclass or subinterface of the Collection interface. The Collection interface lays the ground work for the Collection Framework. The Collection interface defines many essential abstract methods such as add(), clear(), remove(), and of course removeAll(). Each and every subclass or subinterface will inherit these methods and any concrete subclass must implement them. So now we know that a collection must contain a group of elements (objects) and hence why we can pass an object instance that is derived from the Collection interface.
Now the <?> portion of the parameter list is a generic wildcard of unknown type. I will be going over generic wildcards and wildcard bounding in future tutorials after I have completed my ArrayList series. For now, just be sure that the object types contained in the elements of the current instance of the ArrayList match the object types contained in the Collection list argument object.
The c portion of the parameter list merely represents a local copy of the reference variable.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListRemoveAll
C:\Java>cd ArrayListRemoveAll
C:\Java\ArrayListRemoveAll>Notepad ArrayListRemoveAll.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;
class ArrayListRemoveAll {
    public static void main(String args[]) {
        ArrayList<String> states = new ArrayList<String>();
        Collections.addAll(states, "Alaska", "Alabama", "Alabama", "California", "Colorado", "Hawaii", "New York");
        System.out.println("states = " + states);
    
        Collection<String> removeThese = Arrays.asList("Alaska", "Alabama");
        //List<String> removeThese = Arrays.asList("Alaska", "Alabama");
        //ArrayList<String> removeThese = new ArrayList<>();
        //removeThese.add("Alaska");
        //removeThese.add("Alabama");
        System.out.println("removeThese = " + removeThese);
        states.removeAll(removeThese);
        System.out.println("states.removeAll(removeThese), states = " + states);
        System.out.println("\n-------\n");

        states.clear();
        Collections.addAll(states, "Alaska", "Alabama", "Alabama", "California", "Colorado", "Hawaii", "New York");
        System.out.println("states = " + states);
        states.removeAll(Arrays.asList("Alabama")); // better than remove() if you need to ensure all matching elements are removed
        System.out.println("After removeAll, states = " + states);
    }
}

Now switch back to the command prompt (CMD) and type in javac ArrayListRemoveAll.java and press Enter.
Now type in java ArrayListRemoveAll and press Enter.


C:\Java\ArrayListRemoveAll>javac ArrayListRemoveAll.java
C:\Java\ArrayListRemoveAll>java ArrayListRemoveAll
states = [Alaska, Alabama, Alabama, California, Colorado, Hawaii, New York]
removeThese = [Alabama, Alabama]
states.removeAll(removeThese), states = [California, Colorado, Hawaii, New York] 

-----------------

states = [Alaska, Alabama, Alabama, California, Colorado, Hawaii, New York]
After removeAll, states = [Alaska, California, Colorado, Hawaii, New York]



Final thoughts

None


Tutorials