Thursday, July 6, 2023

Difference between TreeSet, LinkedHashSet and HashSet in Java with Example

In Java, the Collection framework provides a variety of classes to store and manipulate data efficiently. Three commonly used classes for storing unique elements are TreeSet, LinkedHashSet, and HashSet. While all three implement the Set interface and offer similar functionality, they differ in their underlying implementations and behavior. This article aims to delve into the characteristics of TreeSet, LinkedHashSet, and HashSet, highlighting their differences through examples and use cases.


HashSet

HashSet is an implementation of the Set interface that provides a simple and efficient way to store unique elements. It does not guarantee the order of elements and does not allow duplicates. HashSet achieves its efficiency by using a hash table internally. The hash table allows constant-time complexity for basic operations like add, remove, contains, and size. However, the order in which elements are stored is not predictable.

Example usage of HashSet:


import java.util.HashSet;

HashSet set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Mango");
set.add("Banana"); // Ignored, as HashSet does not allow duplicates

System.out.println(set); // Output: [Orange, Mango, Banana, Apple]

In the example above, the HashSet stores the elements in an unordered manner, and the duplicate element "Banana" is ignored. 

LinkedHashSet 

LinkedHashSet, like HashSet, stores unique elements but also maintains the insertion order. It achieves this by using a combination of a hash table and a doubly-linked list. The hash table allows constant-time complexity for basic operations, while the linked list ensures that elements are stored in the order they were added.

Example usage of LinkedHashSet:


import java.util.LinkedHashSet;

LinkedHashSet set = new LinkedHashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Mango");
set.add("Banana"); // Ignored, as LinkedHashSet does not allow duplicates

System.out.println(set); // Output: [Apple, Banana, Orange, Mango]

In this example, the LinkedHashSet preserves the order of elements as they were inserted. The duplicate element "Banana" is again ignored. 

TreeSet

TreeSet is an implementation of the SortedSet interface, which means it stores elements in sorted order. TreeSet uses a self-balancing binary search tree, specifically a Red-Black Tree, internally. This data structure allows for efficient searching, insertion, and deletion operations with a time complexity of O(log n). However, maintaining the sorted order requires additional time and space compared to HashSet and LinkedHashSet. 

Example usage of TreeSet:


import java.util.TreeSet;

TreeSet set = new TreeSet<>();
set.add("Apple");
set.add("Banana");
set.add("Orange");
set.add("Mango");
set.add("Banana"); // Ignored, as TreeSet does not allow duplicates

System.out.println(set); // Output: [Apple, Banana, Mango, Orange]

The TreeSet in the example above stores the elements in ascending order. The duplicate element "Banana" is ignored, and the output is sorted accordingly. 

Conclusion

In summary, TreeSet, LinkedHashSet, and HashSet are all useful implementations of the Set interface in Java. HashSet provides fast and efficient operations but does not guarantee the order of elements. LinkedHashSet combines the features of HashSet and maintains the insertion order. TreeSet, on the other hand, ensures elements are stored in a sorted order, but at the cost of additional time and space complexity. Choosing the appropriate class depends on

No comments:

Post a Comment