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.

Example 1

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]

Example 2

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]

Example 3

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 ]]
java_util_arraylist.htm
Advertisements