ArrayList removeAll() method in Java

This post will discuss the removeAll() method of the ArrayList class in Java. removeAll() method removes all of the elements in the specified collection. We can remove only one element from the list using the remove() method.

  • Method declaration – public boolean removeAll(Collection c)
  • What does it do? It will remove all elements present in the specified collection ( passed in the argument ). Here we can pass any class’s object that implements the Collection interface, and the removeAll() method will remove all of the elements if they are present in the ArrayList.
    And if the data type of ArrayList and the collection passed in the arguments are incompatible, then the removeAll() method will throw a ClassCastException.
  • What does it return? It will return true if the list has been modified after calling the function. Otherwise, it will return false.
Code Example

We will use the Vector class, which also implements the Collection interface and will remove the elements present in that vector from the ArrayList.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");
		al.add("third");

		Vector<String> vector = new Vector<String>();
		vector.add("first");
		vector.add("last");

		System.out.println("ArrayList contents before: " + al);
		System.out.println("ArrayList changed? "+al.removeAll(vector));
		System.out.println("ArrayList contents after: " + al);

	}
}

Output –

ArrayList contents before: [first, second, third]
ArrayList changed? true
ArrayList contents after: [second, third]

Here, only one element ( “first” ) of the vector object was present in the ArrayList; hence, it was removed from the list.

Time Complexity of the removeAll() method in ArrayList

The average time complexity of the removeAll() method is O(nm), where n is the number of the elements in the list and m is the number of elements present in the collection passed as an argument.

Some interesting scenarios

What if the ArrayList is not changed after calling the removeAll() method?

Here, we will enter some elements in the vector that will not exist in the list. Thus, no element will be removed from the list on calling the function, and the removeAll() method will return false, as illustrated below.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add("second");
		al.add("third");

		Vector<String> vector = new Vector<String>();
		vector.add("hello");
		vector.add("codekru");

		System.out.println("ArrayList contents before: " + al);
		System.out.println("ArrayList changed? " + al.removeAll(vector)); // this will return "false"
		System.out.println("ArrayList contents after: " + al);

	}
}

Output –

ArrayList contents before: [first, second, third]
ArrayList changed? false
ArrayList contents after: [first, second, third]
How to remove all occurrences of a specified element from an ArrayList?

We can easily do that using the removeAll() method by taking a collection containing only that specified element. Then all occurrences of that element will be removed from the list.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("hello");
		al.add("codekru");
		al.add("hi");
		al.add("codekru");

		Vector<String> vector = new Vector<String>();
		vector.add("codekru");

		System.out.println("ArrayList contents before: " + al);

		// this will remove all occurrences of "codekru" from the list
		System.out.println("ArrayList changed? " + al.removeAll(vector));

		System.out.println("ArrayList contents after: " + al);

	}
}

Output –

ArrayList contents before: [hello, codekru, hi, codekru]
ArrayList changed? true
ArrayList contents after: [hello, hi]
Can we remove null objects from the ArrayList using the removeAll() method?

Yes, we can remove the null objects, but if the specified Collection does not permit the null element, it will throw a NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al.add("first");
		al.add(null);
		al.add("second");
		al.add(null);
		al.add("third");

		Vector<String> vector = new Vector<String>();
		vector.add(null);

		System.out.println("ArrayList contents before: " + al);
		System.out.println("ArrayList changed? " + al.removeAll(vector));
		System.out.println("ArrayList contents after: " + al);

	}
}

Output –

ArrayList contents before: [first, null, second, null, third]
ArrayList changed? true
ArrayList contents after: [first, second, third]
What if we use the removeAll() on a null ArrayList?

It will throw a NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

		ArrayList<String> al = new ArrayList<String>();
		al = null;

		Vector<String> vector = new Vector<String>();
		vector.add("abc");

		System.out.println("ArrayList changed? " + al.removeAll(vector));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.removeAll(java.util.Collection)" because "al" is null

Please visit this link to learn more about the ArrayList class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.