What is Java Set, HashSet, TreeSet and LinkedHashSet?

The Set interface in Java

Just like the List interface, the Set is a Collection in Java. The difference between the two is that the Set does not allow duplicate elements.

Just like the List interface, the Set also has implementation classes like HashSet.

Before showing you examples of using the Set interface, these are a few main points about Set interface in Java.

Important points about Java Set

  • The Set is a subinterface of Collection.
  • It does not allow duplicate elements.
  • It inherits methods from the Collection.
  • The Set adds a strong contract on the behavior of hashCode and equals operations.
  • The index of Set elements starts at 0.
  • As Set is an interface, its object cannot be initialized.
  • The Set is implemented by HashSet, TreeSet, and LinkedHashSet.
  • The Set interface is the part of java.util package|.

The following section demonstrates how Set can be used in Java programs.

A demo of creating set with HashSet class

In this example, a Set is created by using the HashSet class. The HashSet is backed by the hash table that makes no guarantee of the iteration order. Five integer elements are created by add method and displayed by using the for..each loop. You can see the difference of order in adding the elements and as they are displayed:

Java set

See online demo and code

The code of creating and displaying the Set elements:

 

An example of using the LinkedHashSet for creating a Set

You should use the LinkedHashSet class when iteration in insertion order is required. As such, the HashSet does not guarantee the iteration order and the order of elements is unpredictable as compared to insertion order. In certain scenarios, you may want the same order as elements are inserted.

See this example where I created a Set by using the LinkedHashSet class. Four string elements are entered by using the add() method and finally the Set is displayed by using the for..each loop:

set linkedhashset

See online demo and code

The code:

 

You can see the order of displaying the elements is the same as those were inserted.

What happens if duplicate elements are added in a Set?

For demonstrating this scenario, I have created a Set with fruit names. Total six elements are inserted that are fruit names as follows:

fruit_Set.add(“Mango”);

fruit_Set.add(“Banana”);

fruit_Set.add(“Apple”);

fruit_Set.add(“Grapes”);

fruit_Set.add(“Banana”);

fruit_Set.add(“Pine Apple”);

You can see, the Banana is added at 1 and 4 index positions of the Set. Now, have a look at the output for displaying the total number of elements in the set by using the size method and the element values by using a for loop:

set duplicate

See online demo and code

The code of Set in Java with duplicate entries:

 

You can see, the size method is only returned 5 as the total number of elements. As using the for loop for displaying Set Java elements, it displayed Banana once i.e. the first occurrence only.

A demo of using the clear method of set

The clear method of the Set can be used for removing all elements from the specified Set. See this example where I created a Set with four elements. The Set is displayed before and after using the clear() method:

set clear

See online demo and code

The code for clearing the Set objects:

 

Removing a specific element from the Set example

You may also remove only a specific element from the list by using the remove() method. See this example where “Apple” is deleted by using the remove() method:

set remove

See online demo and code

The code:

 

A demo of using iterator object

In above examples, we used for each loop to iterate through the Set elements. You may also use the iterator object for going through a Set.

In this example, a set Java is created by using the LinkedHashSet class. After that, the iterator object is created and a while loop is used as follows:

set iterator

See online demo and code

The code with iterator object:

 

A demo of using TreeSet class for creating a Set

In the following example, a Set is created by using the TreeSet class. The Set of color names is created and displayed:

set treeset

See online demo and code

The code with TreeSet:

 

You can see, an unordered set is created and displayed.