Java ArrayList.removeAll()

 Posted On  | Yashwant Chavan 

In this tutorial you will learn about ArrayList.removeAll() method. boolean java.util.ArrayList.removeAll(Collection<?> c) removes from this list all of its elements that are contained in the specified collection.


Removes from this list all of its elements that are contained in the specified collection.

boolean java.util.ArrayList.removeAll(Collection<?> c)


c collection containing elements to be removed from this list.


true if this list changed as a result of the call


ClassCastException - If the class of an element of this list is incompatible with the specified collection

NullPointerException - If this list contains a null element or if the specified collection is null.

Java ArrayList.removeAll() example

Refer below steps

  • Constructs two ArrayList with an initial capacity using new ArrayList<String>().
  • Add some elements in colors and removeColors ArrayList using add() method.
  • Call removeAll() method on colors ArrayList. It removes all the elements from this (colors) list that are contained in the specified collection. It return true if elements are removed from the list otherwise return false.

import java.util.ArrayList;

public class ArrayListRemoveAll {
    public static void main(String[] args) {

        ArrayList <String> colors = new ArrayList <String> ();

        ArrayList <String> removeColors = new ArrayList <String> ();

        for (String color: colors) {
            System.out.println("colors before removeAll:- " + color);

        System.out.println("\nList removeAll() ? " + colors.removeAll(removeColors) + "\n");

        for (String color: colors) {
            System.out.println("colors after removeAll:- " + color);



colors before removeAll:- Red
colors before removeAll:- Green
colors before removeAll:- Blue
colors before removeAll:- White
colors before removeAll:- Pink

List removeAll() ? true

colors after removeAll:- Red
colors after removeAll:- White
colors after removeAll:- Pink

Promote your Blog, Website, or services on, My paypal id:

Payment will be accepted through paypal only.

© 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0300 seconds.