The Wayback Machine - https://web.archive.org/web/20240618020133/https://www.geeksforgeeks.org/set-retainall-method-in-java-with-example/
Open In App

Set retainAll() method in Java with Example

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

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 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.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of Set
            Set<Integer> arrset1 = new HashSet<Integer>();
  
            // Populating arrset1
            arrset1.add(1);
            arrset1.add(2);
            arrset1.add(3);
            arrset1.add(4);
            arrset1.add(5);
  
            // print arrset1
            System.out.println("Set before retainAll() operation : "
                               + arrset1);
  
            // Creating another object of  Set
            Set<Integer> arrset2 = new HashSet<Integer>();
            arrset2.add(1);
            arrset2.add(2);
            arrset2.add(3);
  
            // print arrset2
            System.out.println("Collection Elements to be retained : "
                               + arrset2);
  
            // Removing elements from arrset
            // specified in arrset2
            // using retainAll() method
            arrset1.retainAll(arrset2);
  
            // print arrset1
            System.out.println("Set after retainAll() operation : "
                               + arrset1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

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

Example 2: For NullPointerException.




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


Output:

Set 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

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#retainAll(java.util.Collection)



Previous Article
Next Article

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
CopyOnWriteArraySet retainAll() method in Java with Example
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 me
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
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
CopyOnWriteArrayList retainAll() method in Java with Examples
The Java.util.concurrent.CopyOnArrayList.retainAll() method in Java is used to retain only the elements in the list that are contained in specific collection. Syntax: public boolean retainAll(Collection col) Parameters: This method accepts a mandatory parameter col which is of the type of collection. This is the collection of elements to be retaine
2 min read
List retainAll() Method in Java with Examples
This method is used to retain all the elements present in the collection from the specified collection into the list. Syntax: boolean retainAll(Collection c) Parameters: This method has only argument, collection of which elements are to be retained in the given list. Returns: This method returns True if elements are retained and list changes. Below
2 min read