
- 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 retainAll() Method
Description
The Java ArrayList retainAll(Collection<?> c) method retains only the elements in this arrayList which are present in the provided collection. Or we can say, this method removes elements from this list which are not contained in the specified collection.
Declaration
Following is the declaration for java.util.ArrayList.retainAll() method
public boolean retainAll(Collection<?> c)
Parameters
c − collection containing elements to be retained in 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.
Retaining Multiple Elements of an ArrayList of Integers while Removing Others Example
The following example shows the usage of Java ArrayList retainAll(collection) method. We're creating an ArrayList of Integers, adding some elements, print it and then use retainAll(collection) method to retain 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 retain two common elements System.out.println("ArrayList modified: " + arrayList.retainAll(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 = [20, 30]
Retaining Multiple Elements of an ArrayList of Strings while Removing Others Example
The following example shows the usage of Java ArrayList retainAll(collection) method. We're creating an ArrayList of Strings, adding some elements, print it and then use retainAll(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 arrayList 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 retain two common elements System.out.println("ArrayList modified: " + arrayList.retainAll(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 = [B, C]
Retaining Multiple Elements of an ArrayList of Objects while Removing Others Example
The following example shows the usage of Java ArrayList retainAll(collection) method. We're creating an ArrayList of Student objects, adding some elements, print it and then use retainAll(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 arrayList 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 retain two common elements System.out.println("ArrayList modified: " + arrayList.retainAll(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 = [[ 1, Julie ], [ 3, Adam ]]