Open In App

CopyOnWriteArraySet retainAll() method in Java with Example

Last Updated : 24 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The retainAll() method of java.util.concurrent.CopyOnWriteArraySet class is used to retain from this set all of its elements that are contained in the specified collection.

Syntax:

public boolean retainAll(Collection c)

Parameters: This method takes collection c as a parameter containing elements to be retained from this set.

Returns Value: This method returns true if this set changed as a result of the call.

Exception: This method throws NullPointerException if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.

Below are the examples to illustrate the retainAll() method.

Example 1:




// Java program to demonstrate
// retainAll() method for Integer value
  
import java.util.concurrent.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
  
            // Creating object of CopyOnWriteArraySet<Integer>
            CopyOnWriteArraySet<Integer>
                set1 = new CopyOnWriteArraySet<Integer>();
  
            // Populating set1
            set1.add(1);
            set1.add(2);
            set1.add(3);
            set1.add(4);
            set1.add(5);
  
            // print set1
            System.out.println("CopyOnWriteArraySet before "
                               + "retainAll() operation : "
                               + set1);
  
            // Creating another object of  CopyOnWriteArraySet<Integer>
            CopyOnWriteArraySet<Integer>
                set2 = new CopyOnWriteArraySet<Integer>();
            set2.add(1);
            set2.add(2);
            set2.add(3);
  
            // print set2
            System.out.println("Collection Elements"
                               + " to be retained : "
                               + set2);
  
            // Removing elements from set
            // specified in set2
            // using retainAll() method
            set1.retainAll(set2);
  
            // print set1
            System.out.println("CopyOnWriteArraySet after "
                               + "retainAll() operation : "
                               + set1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

CopyOnWriteArraySet before retainAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be retained : [1, 2, 3]
CopyOnWriteArraySet after retainAll() operation : [1, 2, 3]

Example 2: For NullPointerException




// Java program to demonstrate
// retainAll() method for Integer value
  
import java.util.concurrent.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
  
            // Creating object of CopyOnWriteArraySet<Integer>
            CopyOnWriteArraySet<Integer>
                set1 = new CopyOnWriteArraySet<Integer>();
  
            // Populating set1
            set1.add(1);
            set1.add(2);
            set1.add(3);
            set1.add(4);
            set1.add(5);
  
            // print set1
            System.out.println("CopyOnWriteArraySet before "
                               + "retainAll() operation : "
                               + set1);
  
            // Creating another object of  CopyOnWriteArraySet<Integer>
            CopyOnWriteArraySet<Integer>
                set2 = null;
  
            // print set2
            System.out.println("Collection Elements"
                               + " to be retained : "
                               + set2);
  
            System.out.println("\nTrying to pass "
                               + "null as a specified element\n");
  
            // Removing elements from set
            // specified in set2
            // using retainAll() method
            set1.retainAll(set2);
  
            // print set1
            System.out.println("CopyOnWriteArraySet after "
                               + "retainAll() operation : "
                               + set1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

CopyOnWriteArraySet before retainAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be retained : null

Trying to pass null as a specified element

Exception thrown : java.lang.NullPointerException


Similar Reads

LinkedHashSet retainAll() method in Java with Example
The retainAll() method of java.util.LinkedHashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true
3 min read
AbstractSet retainAll() method in Java with Example
The retainAll() method of java.util.AbstractSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true i
3 min read
Stack retainAll() method in Java with Example
The retainAll() method of java.util.Stack class is used to retain from this stack all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this stack. Returns Value: This method returns true if
3 min read
HashSet retainAll() method in Java with Example
The retainAll() method of java.util.HashSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true if th
3 min read
AbstractSequentialList retainAll() method in Java with Example
The retainAll() method of java.util.AbstractSequentialList class is used to retain from this list all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this list. Returns Value: This method r
3 min read
TreeSet retainAll() method in Java with Example
The retainAll() method of java.util.TreeSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true if th
3 min read
Set retainAll() method in Java with Example
The retainAll() method of java.util.Set interface is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Return Value: This method returns true if thi
3 min read
CopyOnWriteArraySet toArray() method in Java with Example
toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the array. It acts as a bridge between array-based an
3 min read
CopyOnWriteArraySet containsAll() method in Java with Example
The containsAll() method of Java CopyOnWriteArraySet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C is a Collection. This parameter refers
2 min read
ArrayList retainAll() method in Java
The retainAll() method of ArrayList is used to remove all the array list's elements that are not contained in the specified collection or retains all matching elements in the current ArrayList instance that match all elements from the Collection list passed as a parameter to the method. Syntax: public boolean retainAll(Collection C) Parameters: The
4 min read