English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented(I)

Java Object-Oriented(II)

Java Object-Oriented(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Java other topics

Java ArrayList removeAll() usage and example

Java ArrayList Methods

The Java ArrayList removeAll() method removes all elements from the ArrayList specified by the collection.

The syntax of removeAll() method is:

arraylist.removeAll(Collection c);

The parameter of removeAll()

  • collection - All elements existing in the collection will be removed from the ArrayList.

The return value of removeAll()

  •  If an element is removed from the ArrayList, it returns true

  •  If the class of the existing element in the ArrayList is not compatible with the element class in the specified collection, a ClassCastException is thrown

  •  If the ArrayList contains null elements and the specified collection does not allow null elements, a NullPointerException is thrown

Example1To remove all elements from the ArrayList

import java.util.ArrayList;
class Main {
    public static void main(String[] args){
        //Create ArrayList
        ArrayListlanguages = new ArrayList<>();
        //Add elements to the arraylist
        languages.add("Java");
        languages.add("JavaScript");
        languages.add("Python");
        System.out.println("Programming languages: " + languages);
        //Remove all elements from the ArrayList
        languages.removeAll(languages);
        System.out.println("ArrayList after removeAll(): ", + languages);
    }
}

Output Result

Programming languages: [Java, JavaScript, Python]
The ArrayList after removeAll(): []

In the above example, we created an ArrayList named languages. The ArrayList stores the names of programming languages. Note this line,

languages.removeAll(languages);

Here, we pass the ArrayList languages as the parameter to the removeAll() method. Therefore, this method removes all elements from the ArrayList.

NoteIt is recommended to use the clear() method to remove all elements from the ArrayList. For more information, please visitJava ArrayList clear().

Example2:Delete all elements that exist in one ArrayList from another ArrayList

import java.util.ArrayList;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayListlanguages1 = new ArrayList<>();
        //Insert elements at the end of the arraylist
        languages1.add("Java");
        languages1.add("English");
        languages1.add("C");
        languages1.add("Spanish");
        System.out.println("Languages"1: " + languages1);
        //Create another arraylist
        ArrayListlanguages2 = new ArrayList<>();
        //Add elements to the arraylist
        languages2.add("English");
        languages2.add("Spanish");
        System.out.println("Languages"2: " + languages2);
        //from ArrayList1Delete ArrayList2All elements
        languages1.removeAll(languages2);
        System.out.println("After removeAll(): Languages"1: " + languages1);
    }
}

Output Result

Languages1: [Java, English, C, Spanish]
Languages2: [English, Spanish]
After removeAll(): Languages1: [Java, C]

In the above example, we created two ArrayLists named languages1and languages2Note this line,

languages1.removeAll(languages2);

Here, the removeAll() method is used to delete languages1Also exists in languages2Therefore, English and Spanish exist in languages1Delete.

Example3:Delete all elements from the ArrayList that also exist in the HashSet

import java.util.ArrayList;
import java.util.HashSet;
class Main {
    public static void main(String[] args) {
        //Create an ArrayList
        ArrayListnumbers = new ArrayList<>();
        //Add elements to the ArrayList
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        System.out.println("ArrayList: " + numbers);
        //Create a HashSet
        HashSetprimeNumbers = new HashSet<>();
        //Add elements to the HashSet
        primeNumbers.add(2);
        primeNumbers.add(3);
        System.out.println("HashSet: " + primeNumbers);
        //Remove all elements of HashSet from ArrayList
        numbers.removeAll(primeNumbers);
        System.out.println("ArrayList after removeAll(): ", + numbers);
    }
}

Output Result

ArrayList: [1, 2, 3, 4]
HashSet: [2, 3]
ArrayList after removeAll(): [1, 4]

In the above example, we created an arraylist named numbers and a hash set named primeNumbers. Note this line:

numbers.removeAll(primeNumbers);

In this case, the removeAll() method removes all elements that exist in primeNumbers from numbers. Therefore, the following elements were removed from the arraylist numbers: 2 And 3.

Java ArrayList Methods