Javatpoint Logo
Javatpoint Logo

HashSet Vs LinkedHashSet

The HashSet is a class of Java collection framework used to create a collection that uses a hashtable for storing the object. In contrast, the LinkedHashSet class is similar to the HashSet. Moreover, it maintains the insertion order.

The HashSet inherits the properties of the AbstractSet class and implements the Set interface. The LinkedHashSet inherits the HashSet class and implements the Set interface.

HashSet

The HashSet is a class that implements the Set interface. It is used to store the objects in a hashtable; a hashtable is a data structure, which stores data in an ArrayList. It provides quick access to the data using the array index. We can insert, update, and remove the elements easily using a HashSet.

Below are some features of the HashSet class:

  • HashSet uses a mechanism called "Hashing" to store the elements.
  • It uses a hashtable data structure to store the elements.
  • It contains unique elements.
  • It allows storing the null values.
  • It is a non-synchronized class.
  • It does not provide a mechanism to maintain the insertion order. So the elements will be inserted based on the Hashcode.
  • It is a useful mechanism for the search operation.
  • Bu default, it uses 16 as the initial size of the hashtable.
  • It extends AbstractSet class and implements the Set interface.
  • It also implements the Cloneable and Serializable interface.

Declaration:

A HashSet class can be declared as follows:

Consider the below example to understand the behavior of HashSet:

Output:

Apple
Cat
Elephant
Dog
Banana

From the above example, we can see the elements are stored in an unordered collection.

LinkedHashSet

LinkedHashSet class is quite similar to the HashSet class; it is an ordered version of HashSet. Moreover, it allows us to maintain the insertion order of the elements. It inherits the HashSet class and implements the Set interface. It also uses a hashtable to store the elements. It allows us to easily insert, update, remove, and maintain the elements.

Below are some features of the LinkedHashSet:

  • It contains unique elements.
  • It uses a hashtable & a doubly-linked list to store & maintain the elements.
  • It can have null elements.
  • It provides an option set operation.
  • It is non-synchronized.
  • It allows an easy way to maintain the insertion order.

Declaration:

A LinkedHashSet class can be declared as follows:

Consider the below example to understand the behavior of LinkedHashSet:

Output:

A
B
C
D
E

From the above example, we can see the elements are iterated in insertion order.

Difference between HashSet and LinkedHashSet

Below are some key differences between HashSet and LinkedHashSet:

  • HashSet is an unordered & unsorted collection of the data set, whereas the LinkedHashSet is an ordered and sorted collection of HashSet.
  • HashSet does not provide any method to maintain the insertion order. Comparatively, LinkedHashSet maintains the insertion order of the elements.
  • We can not predict the insertion order in HashSet, but we can predict it in LinkedHashSet.
  • The LinkedHashSet extends the HashSet, so it uses a hashtable to store the elements. Moreover, it uses a doubly linked list to maintain the insertion order.
  • The HashSet and LinkedHashSet both implement the Set interface.
  • HashSet is slightly faster than the LinkedHashSet. But both provide almost similar performance,
  • Both provide o(1) complicity for inserting, removing, retrieving the object.
  • Both the HashSet and LinkedHashSet allows only one null objects.
  • The LinkedHashSet requires more memory than the HashSet.
  • The HashSet was introduced in Java 2, and the LinkedHashSet was introduced in Java 4.

Consider the below tabular differences between HashSet and LinkedHashSet:

Property HashSet LinkedHashSet
Data structure It uses a Hashtable to store the elements. It uses a HashTable and doubly linked list to store and maintain the insertion order of the elements.
Technique to store the elements Hashing Hashing
Insertion Order It does not provide any insertion order. We can not predict the order of elements. It provides an insertion order; we can predict the order of elements.
Null elements It allows only one null element. It also allows only one null element.
Memory It requires less memory. It requires more memory than HashSet.
Performance It provides slightly faster performance than LinkedHashSet It provides low performance than HashSet
Synchronized Non-synchronized Non-synchronized
Complexity for the insertion, removal, retrieval operations O (1) O (1)
Declaration HashSet obj = new HashSet(); LinkedHashSet obj = new LinkedHashSet();
Extends AbstractSet class HashSet class
Implements Set interface Set interface
Initial Capacity 16 16
Package java.util Java.util

When to use HashSet and LinkedHashSet

If we are required to maintain the insertion order, then the LinkedHashSet will be useful. But, when maintaining the insertion order of the elements is not our priority, the HashSet will be useful and provide improved performance.

Summary:

As we have discussed both HashSet and LinkedHashSet classes of the Java collection framework, Adding a final note to this discussion, we would like to say that both data structures are very useful and use a similar implementation technique (Hashing). But, if you have a minor project where the insertion order is not necessary, we will recommend using the HashSet as it provides improved performance than LinkedHashSet. If you want to predict the insertion order, then the use of LinkedHashSet is recommended as it uses a doubly linked list to maintain the order of the element and provides an easy search process.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA