Set Interface Tutorial In Java With Example

Set is the group of elements, or we can say collection of elements, that can never contains Duplicate element. If in case duplicate elements is added then Set Interface will not add it in that group of elements. Set also allow null element and as it avoids duplicity, so only one null is allowed.

The formal definition of the Set interface states that no two elements in the Set are equal such that e1.equals(e2) is never true.

Hierarchy of Set Interface

Set Interface extends Collection Interface which further extends Iterable Interface, as shown in figure 1.

Hierarchy of Set Interface

Set Implementations

1. HashSet

HashSet is the implementation of Set Interface which uses Hash table for storing the data. Hash table internally uses a phenomena known as hashing,  Hash set does not maintains the insertion order, that is when we retrieve values from it  we do not get that values in the same order as we have entered in it.

2. LinkedHashSet

LinkedHashSet is also the implementation of Set Interface. It is almost similar to hashset, but it maintains insertion order i.e. values are retrieved in the same order in which they are added. It uses doubly linked list to obtain this functionality.

For more read LinkedHashSet tutorial

3. TreeSet

TreeSet  also implements Set Interface, it is also similar to hashset , but it stores all the elements in their natural order , like all integer values are stored in ascending order   and strings are stored according to Dictionary values.

Apart from adding this functionality of maintaining natural ordering, TreeSet do not allow null values.

TreeSet is best choice for storing large amount of data, as its retrieval and access time is very fast, which makes data to be found in no time.

For more read TreeSet tutorial

Example of Set Interface

Let us discuss Set Interface with the help of program; following program has been divided into 3 Steps that we will discuss one by one.

import java.util.*;

public class SetInterfaceDemo{

public static void main(String args[]){

Set<Integer> hashSetobject= new HashSet<Integer>();  //Step 1

Set<Integer> linkedHashSetObject = new LinkedHashSet<Integer>();

Set<Integer> treeSetObject= new TreeSet<Integer>();

hashSetobject.add(1);  //Step 2











System.out.println("HashSet object is : " +hashSetobject);  //Step 3

System.out.println("LinkedHashSet object is : " +linkedHashSetObject);

System.out.println("TreeSet object is : " +treeSetObject);
  •  In Step 1, we have created objects of HashSet, LinkedHashSet and TreeSet class, we have defined these objects to store value of Integer type.
  • In Step 2, we have used add method to store values in the data structures that we have created in step 1.
  • In Step 3, we have printed values of these objects.


HashSet object is : [1, 2, 4]

LinkedHashSet object is : [3, 1, 2, 4]

TreeSet object is : [1, 2, 3, 4]

 Importance of Set Interface

  • Set has its own importance like in case we just want to remove all the duplicate elements from list, we can convert that list into set, and can very easily remove all the duplicate elements, and convert back this set to list object.
  • Set has various implementations like HashSet, LinkedHashSet and TreeSet, These implementations are very helpful in storing the elements according to the need, like if we want our data to be stored in natural order than we can use TreeSet, or we want data as it is we have entered than we can use LinkedHashSet.

Some of the key Set Methods

1. boolean add(Object)

This methods adds an element to the set, if and only if that element is not already present in the set, It will return true if element is added, or false if given element is already present and hence it is not added

2. void clear()

This method removes all elements from the Set.

3. boolean contains(Object)

This method returns true if the Set contains the given element in the set, else false if there is no such element.

4. boolean isEmpty() 

This method returns true if the Set contains no elements or false if there are elements present in the set.

5. Iterator iterator()

This method gives an object of Iterator class which can be used to traverse through the Set.

6. boolean remove(Element  e)

This method removes the given element in the argument from the Set and returns true if the argument was removed, else false if the given element is not present.

7. int size() 

This method gives the number of elements in the Set.

8. Object[] toArray()

This methods returns the set in the form of array of type “Object

 Important Points About Set In JAVA

  • Set has three implementation classes HashSet, LinkedHashSet and TreeSet. If we have requirement of storing elements in natural order than we should use TreeSet and if we have requirement of retrieving the elements as it is entered than we should use LinkedHashSet, and if no such requirements are there we can use HashSet.
  • Duplicate values are not allowed
  • Only one null element is allowed as duplicity is not allowed

Set Quick Revision points

  • Duplicates are not allowed
  • Only one Null value is allowed
  • TreeSet is preferred where we want to store data in natural order
  • LinkedHashSet is preferred where we want to maintain insertion order.
  • HashSet is preferred where we just want to store data without any requirement of maintaining any special ordering

Ask Your Question Or Leave A Reply

Your email address will not be published. Required fields are marked *