Java Set Interface

In this tutorial, we will learn about the java.util.Set interface and its behaviour, methods, use cases along with working examples of the Set interface.


The java.util.Set is one of the interfaces in collections framework and which is used to store a distinct-group of elements, in other words Set will allows to add elements into it which are not already present. The Set interface extends the Collection interface and defines a set of methods for working with unordered collections of objects.

Like any other collection interfaces, Set also contains several methods for manipulating its elements, such as add, remove, and contains. However, unlike the lists and other ordered collections, sets do not allow duplicate elements. In addition, the Set interface does not specify the order in which the elements are stored.

It is recommended to override the hashCode and equals for custom type elements(user defined class objects) to find duplicates, which allows to find duplicate on custom logic.

The Set interface has several implementations, including HashSet, TreeSet, and LinkedHashSet. Each implementation has its own unique characteristics and performance characteristics.

Set interface methods

The following are the methods of Set interface and description of each method.

boolean add(E element)Adds the specified element in to the set if it is not already present in set.
boolean remove(Object element)Removes the specified element from the set, if it is present.
boolean contains(Object element)Returns true if the set contains the specified element.
int size()Returns the number of elements in the set.
boolean isEmpty()Returns true if the set contains no elements.
void clear()Removes all the elements from the set.

Use Cases:

The following are the few use cases of Set.

  1. In data processing applications, set is commonly used for elimination of duplicates.
  2. In graph algorithm, by using set helps to keep track of visited vertex.
  3. Online transactions, helps to avoid duplicate the same order in case of connectivity issues or so.

Set example with in-built class:

The following is an example of how to create and use a HashSet object, which implements the Set interface.

CopiedCopy Code
import java.util.HashSet;
import java.util.Set;
public class SetExample {
    public static void main(String[] args) {
        // Create a HashSet object
        Set<String> set = new HashSet<String>();
        // Add elements to the set
        // Print the contents of the set
        System.out.println(set);  // Output: [Red, Orange, Green]
        // Check if an element is in the set
        boolean containsRed = set.contains("Red");
        System.out.println("Set contains Red Color: " + containsRed);  // Output: Set contains Red Color: true
        // Remove an element from the set
        System.out.println(set);  // Output: [Red, Green]

Output :

[Red, Orange, Green]
Set contains Red Color: true
[Red, Green]


In the above example, we referenced an HashSet object to Set and add three elements to it. We then print all the elements of the set and check if it contains the element "Red".

We remove an element from the set and prints all its content.

Set example with custom class:

In this example, we will walk through on custom class object adding to Set with equals and hashCode overridden.

CopiedCopy Code
import java.util.HashSet;
import java.util.Set;
public class SetCustomClass {
	public static void main(String[] args) {
		Set<Hotel> hotels = new HashSet<Hotel>();
		Hotel hotel1 = new Hotel("Five Star","Delhi");
		Hotel hotel2 = new Hotel("Three Star","Delhi");
		Hotel hotel3 = new Hotel("Four Star","Delhi");
		Hotel hotel4 = new Hotel("Five Star","Delhi");
// Returns false as hotel Five Star is already present in same location
	static class Hotel {
		public String hotelName;
		public String location;
		public Hotel(String hotelName, String location) {
			this.hotelName = hotelName;
			this.location = location;
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((hotelName == null) ? 0 : hotelName.hashCode());
			result = prime * result + ((location == null) ? 0 : location.hashCode());
			return result;
		public boolean equals(Object obj) {
			Hotel other = (Hotel) obj;
			if (hotelName.equals(other.hotelName) && location.equals(other.location))
				return true;
			return false;
		public String toString() {
			return "Hotel [hotelName=" + hotelName + ", location=" + location + "]";


[Hotel [hotelName=Four Star, location=Delhi], Hotel [hotelName=Five Star, location=Delhi], Hotel [hotelName=Three Star, location=Delhi]]


We have created a custom class named as hotel, overridden the method of equals and hashCode. Whenever elements are going to added into the Set it checks hashCode of specific elements, if returned hashCode is not present then adds it. If its already present then calls equals method, if this also returns true then will not add specific element otherwise adds to the Set. Same happened in above example while trying to add Five Start hotel from same location as Delhi.


In this tutorial, we covered what is a Set interface, its behaviour and methods associated with it along with possible use cases of set where its being used most and working examples including custom class elements by overriding equals and hashCode methods.