Java ArrayList removeAll() method with example

Java ArrayList removeAll() method Removes from this list all of its elements that are contained in the specified collection.

Method Signature

public boolean removeAll(Collection<?> c)

Specified by:

removeAll method in interface Collection

Specified by:

removeAll method in interface List


Overrides: removeAll method in class AbstractCollection


c – collection containing elements to be removed from this list


true if this list changed as a result of the call

Throws Exception:

ClassCastException – if the class of an element of this list is incompatible with the specified collection (optional)

NullPointerException – if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null

See Also:Collection.contains(Object)

ArrayList removeAll() method Implementation

public boolean removeAll(Collection<?> c) {
        return batchRemove(c, false);
private boolean batchRemove(Collection<?> c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
        return modified;

Java ArrayList removeAll() method example


import java.util.ArrayList;

public class RemoveAllExample {

	public static void main(String[] args) {
		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>();

		// add() method to add Integer element in the list

		// print the values of list
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
	ArrayList<Integer> numericList2 = new ArrayList<Integer>();

		System.out.println("List After value removed : ");
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : " + numbers);
		ArrayList<Integer> numericList3 = null;
		//throw NullPointerException




Numbers : 10
Numbers : 15
Numbers : 20
Numbers : 25
Numbers : 30
List After value removed :
Numbers : 10
Numbers : 20
Numbers : 30
Exception in thread “main” java.lang.NullPointerException
at java.util.Objects.requireNonNull(
at java.util.ArrayList.removeAll(

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList addAll(int index, Collection c) method with example
  2. Java ArrayList add(int index, E element) method with example
  3. Java ArrayList ensureCapacity() method with example
  4. Java ArrayList contains() method with example
  5. Java ArrayList clone() method with example
  6. Java ArrayList indexOf() method with example
  7. Java ArrayList get() method with example
  8. Java ArrayList add(E e) method with example
  9. Java ArrayList removeAll() method with example
  10. Java ArrayList remove() method with example
  11. Java ArrayList addAll(Collection< ? extends E> c) method with example
  12. Java ArrayList forEach() method with example
  13. Java ArrayList isEmpty() method with example
  14. Java ArrayList remove(Object o) method with example
  15. Java ArrayList clear() method with example

Be the first to comment

Leave a Reply