Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the CopyOnWriteArraySet.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.

Overview

The retainAll() method removes all the elements of the CopyOnWriteArraySet object that are not present in the passed collection. In other words, the retainAll() method will retain elements that are present in the collection that is passed as an argument. All other elements that are not present in the argument are removed.

Note: CopyOnWriteArraySet is a thread-safe version of SetA collection that contains no duplicate elements.. CopyOnWriteArraySet internally uses CopyOnWriteArrayList for its operations. For all the write operations like add, set, etc., it makes a fresh copy of the underlying array and operates on the cloned array. Read more about CopyOnWriteArraySet here.

Syntax

The syntax of the retainAll method is given below:

public boolean retainAll(Collection<?> c)

Parameters

This method takes the collection object to be retained in the set 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 set changes as a result of the call, meaning that an element is removed from the set 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.CopyOnWriteArraySet;
import java.util.ArrayList;
class RetainAll {
public static void main( String args[] ) {
// create a CopyOnWriteArraySet objec which can store ineger vaules
CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet<>();
// add elements to the set
set.add(1);
set.add(2);
set.add(3);
// create a list, which contins the element to be retained in the created CopyOnWriteArraySet object
ArrayList<Integer> elementToBeRetained = new ArrayList<>();
elementToBeRetained.add(1);
elementToBeRetained.add(4);
System.out.println("The CopyOnWriteArraySet is "+ set);
System.out.println("The elementToBeRetained is "+ elementToBeRetained);
// use retainAll method to retain only the elements of the elementToBeRetained list
System.out.println("\nCalling set.retainAll(elementToBeRetained). Is list changed - " + set.retainAll(elementToBeRetained));
System.out.println("\nThe set is "+ set);
}
}
Using the retainAll() method

Explanation

  • Lines 1–2: We import the CopyOnWriteArraySet and ArrayList classes.

  • Lines 6–10: We create a new CopyOnWriteArraySet object with the name set and add three elements (1,2,3) to the set object with the add() method.

  • Lines 13–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.

  • Line 21: We use the retainAll() method to only retain the elements that are present in elementToBeRetained. In our case, the elements of set and elementToBeRetained are as follows:

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

The retainAll() method will:

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

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

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

The resulting set will be [1].

RELATED TAGS

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