coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList retainAll() method

Let’s see the retainAll() method of the ArrayList class in Java.

This method is used to retain all the elements in of the collection in the list.

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

ArrayList is an ordered collection i.e. it maintains the insertion order of the elements. Also, it allows duplicate elements in the list.

Also, We have seen a few methods of the ArrayList class before like add(), addAll(), remove(), set(), iterate(), clear(), contains(), get() and removeRange() method.

Today we will see the retainAll() method.

ArrayList retainAll(Collection c) method

The retainAll() method retains all the elements of the collections passed as a parameter in the list.

That means it will remove all elements from the list other than the ones that are present in the collection that is passed as a parameter.

The method signature is given below.

1
public boolean retainAll(Collection<?> c)

As shown in the method signature, it accepts only one parameter i.e. Collection c. It is a collection of the objects that we have to retain in the list.

Also, the retainAll() method has a return type as a boolean. It returns true only if the list is changed as a result of the method call otherwise false.

Let’s see the Java program for a better understanding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.List;

/**
 * A Java program to explain the retainAll() method of the ArrayList class.
 * 
 * @author coderolls.com
 *
 */
public class ArrayListRetainAllExample {

  public static void main(String[] args) {
    //create an empty ArrayList of Integer to retain elements
    List<Integer> numbers1 = new ArrayList<Integer>();
    
    //add number to the list
    numbers1.add(4);
    numbers1.add(5);
    numbers1.add(6);
    
    //create an empty ArrayList of Integer
    List<Integer> numbers2 = new ArrayList<Integer>();
    
    // add the number to the list
    numbers2.add(1);
    numbers2.add(2);
    numbers2.add(3);
    numbers2.add(4);
    numbers2.add(5);
    numbers2.add(6);
    numbers2.add(7);
    numbers2.add(8);
    numbers2.add(9);
    
    System.out.println("Lists before the method call");
    System.out.println("numbers1: "+ numbers1);
    System.out.println("numbers2: "+ numbers2);
    
    //retain all the elements of numbers1 in the list numbers2
    numbers2.retainAll(numbers1);
    
    System.out.println("\nLists after the method call");
    System.out.println("numbers1: "+ numbers1);
    System.out.println("numbers2: "+ numbers2);	
  }
}

Output:

1
2
3
4
5
6
7
Lists before the method call
numbers1: [4, 5, 6]
numbers2: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Lists after the method call
numbers1: [4, 5, 6]
numbers2: [4, 5, 6]

Exceptions thrown by retainAll() Methods

This method may throw the ClassCastException and NullPointerException.

ClassCastException

This method may throw the ClassCastException, if the class of an element of this list is incompatible with the specified collection. (optional)

NullPointerException

This method may throw the 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.

The Java example for the NullPointerException is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.List;
/**
 * A Java program to show the NullPointerException in retainAll() method
 * of the ArrayList class.
 *  
 * @author coderolls.com
 *
 */
public class RetainAllNullPointerExceptionExample {

  public static void main(String[] args) {
    //create an empty ArrayList of Integer to retain elements
    List<String> numbers1 = null;
    
    //create an empty ArrayList of Integer
    List<Integer> numbers2 = new ArrayList<Integer>();
    
    //Add the number to the list
    numbers2.add(1);
    numbers2.add(2);
    numbers2.add(3);
    numbers2.add(4);
    
    System.out.println("Lists before the method call");
    System.out.println("numbers1: "+ numbers1);
    System.out.println("numbers2: "+ numbers2);
    
    //retain all the elements of numbers1 in the list numbers2
    numbers2.retainAll(numbers1);
    
    System.out.println("\nLists after the method call");
    System.out.println("numbers1: "+ numbers1);
    System.out.println("numbers2: "+ numbers2);	
  }
}

Output:

1
2
3
4
5
6
7
Lists before the method call
numbers1: null
numbers2: [1, 2, 3, 4]
Exception in thread "main" java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at java.util.ArrayList.retainAll(ArrayList.java:714)
	at com.gaurav.ExProject.ArrayList.RetainAllClassCastExceptionExample.main(RetainAllClassCastExceptionExample.java:27)

Conclusion

The retainAll(Collection c) method retains all the elements of the collection passed as a parameter in the list.

1
public boolean retainAll(Collection<?> c)

This method returns true if the list is changed otherwise false.


The example Java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus