What is the HashSet.retainAll() method in Java?
The HashSet.retainAll() method is present in the HashSet class inside the java.util package.
It is used to retain all the elements in a HashSet that are specified in the input collection.
Syntax
public boolean retainAll(Collection c)
This method throws
NullPointerExceptionif the input collection contains a null element and theHashSetdoes not permit null elements, or if the specified collection is null.
Example
Let’s understand with the help of some examples:
-
Suppose that a
HashSetcontains[1, 8, 5, 3, 0]and the input collection contains[0, 3, 1]. After usingHashset.retailAll(collection), theHashSetwill only retain the elements of the collection. So, theHashSetcontains[1, 0, 3]. -
Suppose that the
HashSetcontains[1, 6, 5, 9, 3, 2]and the collection isnull. In this case, aNullPointerExceptionis thrown.
Parameters
The HashSet.retainAll() method accepts one parameter:
Collection: This specifies the collection that needs to be retained within theHashSet.
Return value
The HashSet.retainAll() method returns true if the HashSet gets changed as a result of calling the method.
Code
Let’s have a look at the code.
import java.io.*;import java.util.HashSet;class Main{public static void main(String args[]){try{HashSet<Integer> hash_set1 = new HashSet<Integer>();hash_set1.add(1);hash_set1.add(8);hash_set1.add(5);hash_set1.add(3);hash_set1.add(0);HashSet<Integer> hash_set2 = new HashSet<Integer>();hash_set2.add(8);hash_set2.add(0);hash_set2.add(3);hash_set2.add(5);System.out.println("hash_set1 before retainAll() implementation: "+hash_set1);System.out.println("Collection that needs to be retained: "+hash_set2);hash_set1.retainAll(hash_set2);System.out.println("hash_set1 after retainAll() implementation: "+hash_set1);System.out.println();HashSet<Integer> hash_set3 = null;System.out.println("hash_set2 before retainAll() implementation: "+hash_set2);System.out.println("Collection that needs to be retained: "+hash_set3);hash_set2.retainAll(hash_set3);System.out.println("hash_set2 after retainAll() implementation: "+hash_set2);}catch(NullPointerException e){System.out.println("Exception thrown : " + e);}}}
Explanation:
-
In lines 1 and 2, we imported the required packages and classes.
-
In line 5, we created a
Mainclass. -
In line 7, we created a
main()function. -
In line 8, we created a
tryblock. -
In line 11, we declared a
HashSetofIntegertype, i.e.,hash_set1. -
From lines 11 to 15, we added the elements into the
HashSetby using theHashSet.add()method. -
In line 17, we declare a
HashSetofIntegertype, i.e.,hash_set2. -
From lines 18 to 21, we added the elements into the
HashSetby using theHashSet.add()method. -
In line 23, we displayed the
HashSetbefore callingretainAll()with a message. -
In line 24, we displayed the
Collectionthat needs to be retained fromHashSetwith a message. -
In line 26, we used the
retainAll()function to retain the collection from theHashSet. -
In line 27, we display the
HashSetafter callingretainAll()with a message. -
In line 30, we declare a
HashSetofIntegertype, i.e.,hash_set3, and assign it anullvalue. -
In line 32, we display the
HashSetbefore callingretainAll()with a message. -
In line 33, we display the
Collectionthat needs to be retained fromHashSetwith a message. -
In line 35, we called the
retainAll()function to retain the collection from theHashSet. -
In line 36, we display the
HashSetafter callingretainAll()with a message. -
In line 39, we made a
catchblock to encounterNullPointerExceptionand display the exception when encountered in thetryblock.
This is how to use the HashSet.retainAll() function to retain all the elements in the HashSet specified in the collection from the HashSet.