
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java ArrayList removeAll() Method
Description
The Java ArrayList removeAll(Collection<?> c) method removes all of common elements present in the ArrayList object and the provided collection's elements. This method modifies the ArrayList object.
Declaration
Following is the declaration for java.util.ArrayList.removeAll() method
public boolean removeAll(Collection<?> c)
Parameters
c − collection containing elements to be removed from this list.
Return Value
This method returns true if this list changed as a result of the call.
Exception
ClassCastException − if the class of an element of this list is incompatible with the specified collection.
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.
Removing Multiple Elements from an ArrayList of Integers Example
The following example shows the usage of Java ArrayList removeAll(collection) method. We're creating an ArrayList of Integers, adding some elements, print it and then use removeAll(collection) method to remove few elements. As ArrayList is modified it is printed to check if specified elements are removed or not.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<Integer> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(20); arrayList.add(15); arrayList.add(30); arrayList.add(45); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove two common elements System.out.println("ArrayList modified: " + arrayList.removeAll(Arrays.asList(11,30,20,12))); // let us print all the elements available in arrayList again System.out.println("ArrayList = " + arrayList); } }
Output
Let us compile and run the above program, this will produce the following result −
ArrayList = [20, 15, 30, 45] ArrayList modified: true ArrayList = [15, 45]
Removing Multiple Elements from an ArrayList of Strings Example
The following example shows the usage of Java ArrayList removeAll(collection) method. We're creating an ArrayList of Strings, adding some elements, print it and then use removeAll(collection) method to remove few elements. As ArrayList is modified it is printed to check if specified elements are removed or not.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<String> arrayList = new ArrayList<>(); // use add() method to add elements in the deque arrayList.add("A"); arrayList.add("B"); arrayList.add("C"); arrayList.add("D"); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove two common elements System.out.println("ArrayList modified: " + arrayList.removeAll(Arrays.asList("B","C","E"))); // let us print all the elements available in arrayList again System.out.println("ArrayList = " + arrayList); } }
Output
Let us compile and run the above program, this will produce the following result −
ArrayList = [A, B, C, D] ArrayList modified: true ArrayList = [A, D]
Removing Multiple Elements from an ArrayList of Objects Example
The following example shows the usage of Java ArrayList removeAll(collection) method. We're creating an ArrayList of Student objects, adding some elements, print it and then use removeAll(collection) method to remove few elements. As ArrayList is modified it is printed to check if specified elements are removed or not.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<Student> arrayList = new ArrayList<>(); // use add() method to add elements in the deque arrayList.add(new Student(1, "Julie")); arrayList.add(new Student(2, "Robert")); arrayList.add(new Student(3, "Adam")); arrayList.add(new Student(4, "Jene")); // let us print all the elements available in arrayList System.out.println("ArrayList = " + arrayList); // it will remove two common elements System.out.println("ArrayList modified: " + arrayList.removeAll(Arrays.asList(new Student(1, "Julie"),new Student(3, "Adam")))); // let us print all the elements available in arrayList again System.out.println("ArrayList = " + arrayList); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result −
ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ]] ArrayList modified: true ArrayList = [[ 2, Robert ], [ 4, Jene ]]