The containers in Java play a significant role in containing the important information and records in a sorted manner. Each container has some associated methods as well to add and retrieve the elements conveniently. The “Set” is one such collection/container that stands out by omitting the duplication of the values.
What is a Set in Java?
“Set” is found in the “java.util” package that extends the “Collection” interface. It is an object collection that is unordered and does not support duplicate values/items.
Set Hierarchy
In the above illustration, the items are sorted in the Tree Set.
Set Methods
Method | Functionality |
---|---|
add() | It adds a particular item to the set. |
addAll(container) | It is utilized to append all the items from the argument container to the existing set. |
contains() | It checks if a particular item is present in the set. |
containsAll() | This method checks if the set comprises all the items in the provided collection or not. |
clear() | It removes all items from the set but does not delete the set. |
isEmpty() | It gives true if the collection/container has no items. |
retainAll(container) | This method retains all the items from the set which are specified in the provided container. |
remove() | It removes the provided item from the set. |
removeAll() | This method removes all the items from the collection. |
size() | It fetches the set’s size. |
iterator() | It retrieves an Iterator object for the collection, which can be utilized to give an object. |
Note: The HashSet is an implementation of a Set therefore in this blog, the HashSet will be applied with Set methods.
Example 1: Creating a Set in Java
This code example demonstrates how to create a set and add the values in it:
import java.util.*;
public class Javast {
public static void main(String[] args){
HashSet<Integer> items = new HashSet<>();
items.add(1);
items.add(2);
items.add(3);
System.out.println(items);
}}
In this code:
- First of all, import the given package to access all the features in the “java.util” package.
- Now, create a HashSet of “Integer” data type.
- After that, append the stated integers in it using the “add()” method.
- Lastly, return the resultant HashSet.
Output

The added elements are retrieved appropriately.
Example 2: Applying the Set Methods to Return the Union, Intersection and Difference of the Sets
This demonstration implements the Set methods to compute the union, intersection, and difference of the defined two sets:
import java.util.*;
public class Javast {
public static void main(String args[]){
HashSet<Integer> items1 = new HashSet<Integer>();
items1.addAll(Arrays.asList(new Integer[] {1, 2, 3}));
System.out.println(items1.isEmpty());
HashSet<Integer> items2 = new HashSet<Integer>();
items2.addAll(Arrays.asList(new Integer[] {3, 4, 5}));
System.out.println(items2.isEmpty());
HashSet<Integer> unionSets = new HashSet<Integer>(items1);
unionSets.addAll(items2);
System.out.println("Union of the given two Sets -> " +unionSets);
HashSet<Integer> intersectionSets = new HashSet<Integer>(items1);
intersectionSets.retainAll(items2);
System.out.println("Intersection of the given two Sets -> " +intersectionSets);
HashSet<Integer> differenceSets = new HashSet<Integer>(items1);
differenceSets.removeAll(items2);
System.out.println("Difference of the given two Sets -> " + differenceSets);
}}
According to this block of code:
- Create a HashSet of Integer type and add the stated integers in it via “Integer” object as a list.
- Also, check if the HashSet is empty via the “isEmpty()” method.
- Likewise, create another HashSet and repeat the same operations with it as well.
- Now, create another HashSet to contain the union of the two sets that contains the former defined set elements.
- After that, associate the “addAll()” method with this HashSet to add all the elements of the latter HashSet to return the union of both the sets as a new set.
- Note: Since there is no duplication in the sets, the union will be retrieved appropriately.
- Now, create another HashSet to accumulate the intersection of the sets having the former defined HashSet.
- In the next step, apply the “retainAll()” method to remove all the elements from the latter HashSet that are not contained in the former HashSet, thereby retrieving the intersection of the sets as a new set.
- Lastly, compute the difference of the two HashSets by returning the elements that are unique in the former HashSet.
Output

From this outcome, it can be verified that the corresponding values are retrieved accordingly.
Conclusion
“Set” is an unordered objects collection in which duplicate values cannot be contained and is present in the “java.util” package. The “HashSet” is the implementation of the Set. The set also comprises various methods to compute the union, intersection, and difference etc of the given sets.