HashSet vs LinkedHashSet: Difference Between HashSet and LinkedHashSet in Java

In this article, we will learn the difference between HashSet vs LinkedHashSet in Java with examples.

HashSet is a member of the Java Collections Framework and is a collection that implements the Set interface. It is designed to store unique elements without any duplicate values.

LinkedHashSet is also a part of the Java Collections Framework and is a collection that implements the Set interface. It extends HashSet but additionally maintains a doubly-linked list across all its elements.

Key Points of Comparison

Underlying Data Structure: 

HashSet: Uses a hash table for storage. The internal structure is optimized for fast insertion, deletion, and search operations. 

LinkedHashSet: LinkedHashSet uses a hybrid of a hash table and a linked list. The hash table provides the uniqueness of the set, while the linked list maintains the insertion order.

Order of Elements: 

HashSet: This doesn't guarantee any specific order for its elements. The order can even change over time. 

LinkedHashSet: Maintains the insertion order of elements. LinkedHashSet preserves the order in which elements are inserted into the set. This insertion order is not affected if an element is re-inserted into the set.

Performance:

HashSet: HashSet gives better performance than the LinkedHashSet.

LinkedHashSet: The performance is slightly on the slower side as it also maintains LinkedList internally to maintain the insertion order of elements.

Memory Overhead: 

HashSet: Has a lower memory overhead since it doesn't maintain any order. 

LinkedHashSet: Requires additional memory to maintain the linked list representing the insertion order.

Examples: 

Using HashSet:

Set<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple"); // Duplicates are ignored

System.out.println(fruits);
Output:
[Cherry, Banana, Apple]
Note: The order can be different since HashSet doesn't guarantee any order. 

Using LinkedHashSet:

Set<String> fruits = new LinkedHashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple"); // Duplicates are ignored

System.out.println(fruits);
Output:
[Apple, Banana, Cherry]
Note: The order of insertion is preserved in LinkedHashSet.

When to Use Which? 

HashSet: 

  • When you don't require any ordering for the elements. 
  • When you're conscious about memory usage. 
  • When you want optimal performance without any added overhead. 

LinkedHashSet: 

  • When you need to maintain the insertion order of elements. 
  • When the requirement of preserving order outweighs the slight overhead in terms of performance and memory. 
  • When you want features of both HashSet (elimination of duplicates) and LinkedList (order).

Summary Table

Conclusion

Both HashSet and LinkedHashSet have their unique advantages. The decision to use one over the other boils down to specific use cases. If you're building an application where the order is critical, LinkedHashSet is the way to go. However, for all general purposes where order isn't a concern, HashSet can be a marginally more efficient choice.

Comments