Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

retainall
java
communitycreator

What is the CopyOnWriteArrayList.retainAll method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The retainAll method retains elements that are present in the collection which is passed as an argument. In other words, the retainAll method will remove all the elements of the CopyOnWriteArrayList object that are not present in the passed collection.

CopyOnWriteArrayList is a thread-safe version of an ArrayList. For all the write operations like add, set, etc., it makes a fresh copy of the underlying array and performs the operations in the cloned array. Due to this, the performance is lower when compared to ArrayList. Read more about CopyOnWriteArrayList here.

Syntax

The syntax of the retainAll method is given below:

public boolean retainAll(Collection<?> c) throws ClassCastException

Parameters

This method takes the Collection object to be retained in the list object as an argument.

The method throws ClassCastException if any one of the elements in the passed collection is incompatible with the specified collection.

Return value

The retainAll method returns true if the list changes as a result of the call, i.e. if an element is removed from the list because it is not present in the passed collection. Otherwise, the method returns false.

Code

The code below demonstrates how to use the retainAll method.

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.ArrayList;
class RetainAll {
public static void main( String args[] ) {
// create a CopyOnWriteArrayList objec which can store ineger vaules
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
// add elements to the list
list.add(1);
list.add(2);
list.add(3);
// create a list, which contins the element to be retained in the created CopyOnWriteArrayList object
ArrayList<Integer> elementToBeRetained = new ArrayList<>();
elementToBeRetained.add(1);
elementToBeRetained.add(4);
System.out.println("The CopyOnWriteArrayList is "+ list);
System.out.println("The elementToBeRetained is "+ elementToBeRetained);
// use retainAll method to retain only the elements of the elementToBeRetained list
System.out.println("\nCalling list.retainAll(elementToBeRetained). Is list changed - " + list.retainAll(elementToBeRetained));
System.out.println("\nThe list is "+ list);
}
}
Using retainAll method

Explanation

In the code above:

  • In lines 1 to 2, we import the CopyOnWriteArrayList and ArrayList classes.

  • In lines 6 to 10, we create a new CopyOnWriteArrayList object with the name list, and add three elements (1,2,3) to the list object with the add method.

  • In lines 13 to 15, we create a new ArrayList object with the name elementToBeRetained, and add two elements (1,4) to the elementToBeRetained object with the add method.

  • In line 21, we use the retainAll method to retain only the elements that are present in elementToBeRetained. In our case, the elements of the list and elementToBeRetained are as follows:

Elements of list -- 1,2,3
Elements of elementToBeRetained -- 1,4

The retainAll method will:

  • Check if element 1 of list is present in elementToBeRetained. elementToBeRetained contains 1, so 1 is retained in the list.

  • Check if element 2 of list is present in elementToBeRetained. elementToBeRetained doesn’t contain 2, so 2 is removed from the list.

  • Check if element 3 of list is present in elementToBeRetained. The elementToBeRetained doesn’t contain 3, so 3 is removed from the list.

The resulting list will be [1].

RELATED TAGS

retainall
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring