Saturday, January 17, 2026
HomeLanguagesJavaHashSet retainAll() method in Java with Example

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 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 HashSet<Integer>
            HashSet<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("HashSet before "
                               + "retainAll() operation : "
                               + arrset1);
  
            // Creating another object of  HashSet<Integer>
            HashSet<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("HashSet after "
                               + "retainAll() operation : "
                               + arrset1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

HashSet before retainAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be retained : [1, 2, 3]
HashSet 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 HashSet<Integer>
            HashSet<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("HashSet before "
                               + "retainAll() operation : "
                               + arrset1);
  
            // Creating another object of  HashSet<Integer>
            HashSet<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("HashSet after "
                               + "retainAll() operation : "
                               + arrset1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

HashSet 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
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
118 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS