The syntax of the removeAll()
method is:
arraylist.removeAll(Collection c);
Here, arraylist is an object of the ArrayList class.
removeAll() Parameters
The removeAll()
method takes a single parameter.
- collection -all elements present in collection are deleted from the arraylist.
removeAll() Return Value
- returns
true
if elements are deleted from the arraylist - throws
ClassCastException
if the class of elements present in arraylist is incompatible with the class of elements in specified collection - throws
NullPointerException
if the arraylist contains null element and the specified collection does not allow null elements
Example 1: Remove all elements from an ArrayList
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create an arraylist
ArrayList<String> languages = new ArrayList<>();
// add elements to arraylist
languages.add("Java");
languages.add("JavaScript");
languages.add("Python");
System.out.println("Programming Languages: " + languages);
// remove all elements from arraylist
languages.removeAll(languages);
System.out.println("ArrayList after removeAll(): " + languages);
}
}
Output
Programming Languages: [Java, JavaScript, Python] ArrayList after removeAll(): []
In the above example, we have created an arraylist named languages. The arraylist stores the name of programming languages. Notice the line,
languages.removeAll(languages);
Here, we are passing the ArrayList languages as an argument of the removeAll()
method. Hence, the method removes all the elements from the arraylist.
Note: The clear()
method is preferred to remove all elements from the arraylist. To know more, visit Java ArrayList clear().
Example 2: Remove all Elements from an ArrayList Present in Another ArrayList
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<String> languages1 = new ArrayList<>();
// insert element at the end of arraylist
languages1.add("Java");
languages1.add("English");
languages1.add("C");
languages1.add("Spanish");
System.out.println("Languages1: " + languages1);
// create another arraylist
ArrayList<String> languages2 = new ArrayList<>();
// add elements to the arraylist
languages2.add("English");
languages2.add("Spanish");
System.out.println("Languages2: " + languages2);
// remove all elements of ArrayList2 from ArrayList1
languages1.removeAll(languages2);
System.out.println("Languages1 after removeAll(): " + languages1);
}
}
Output
Languages1: [Java, English, C, Spanish] Languages2: [English, Spanish] Languages1 after removeAll(): [Java, C]
In the above example, we have created two arraylists named languages1 and languages2. Notice the line,
languages1.removeAll(languages2);
Here, the removeAll()
method is used to remove all those elements from languages1 that are also present in languages2. Hence, English and Spanish are removed from languages1.
Example 3: Remove all Elements from an ArrayList Present in a HashSet
import java.util.ArrayList;
import java.util.HashSet;
class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();
// add element to ArrayList
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
System.out.println("ArrayList: " + numbers);
// create a HashSet
HashSet<Integer> primeNumbers = 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
ArrayList: [1, 2, 3, 4] HashSet: [2, 3] ArrayList after removeAll(): [1, 4]
In the above example, we have created an arraylist named numbers and a hashset named primeNumbers. Notice the line,
numbers.removeAll(primeNumbers);
Here, the removeAll()
method removes all those elements from numbers that are also present in primeNumbers. Hence, 2 and 3 are removed from the arraylist numbers.
Also Read: