HashSet v/s LinkedHashSet v/s TreeSet

In this article, we will compare important implementation classes of Set i.e.; HashSet v/s LinkedHashSet v/s TreeSet

So let’s us discuss in tabular format;

 

HashSet v/s LinkedHashSet v/s TreeSet:

 HashSet LinkedHashSet TreeSet
Uses hash table to store element/objects where duplicates are NOT allowed Uses combination of (hash table + LinkedList) to store element/objects where duplicates are NOT allowed Uses balanced tree to store element/objects where duplicates are NOT allowed
Insertion order is NOT maintained, as it uses hashing technique to store element/objects Insertion order is maintained, as it uses doubly-linked list to store element/objects Insertion order is NOT maintained, as element/objects are stored according to some sorting order
HashSet doesn’t deal with  sorting order; but it can be converted to TreeSet to store element/objects in some sorting order

TreeSet ts = new TreeSet(hashSet);

LinkedHashSet doesn’t deal with  sorting order; but it can be converted to TreeSet to store element/objects in some sorting order

TreeSet ts = new TreeSet(linkedHashSet);

Element/objects stored in TreeSet are according to some sorting order; it could be either default natural sorting order or programmer defined customized sorting order
While iterating HashSet, we will get items in random order While iterating LinkedHashSet, we will get items as per insertion order While iterating TreeSet, we will get items in sorted order;  either natural ordering or customized sorting order
This is introduced in original collection framework in Java 1.2 version This is introduced in Java 1.4 version This is also introduced in original collection framework in Java 1.2 version
Allows NULL insertion but maximum of only one NULL value Allows NULL insertion but maximum of only one NULL value From Java 1.7 version, NULL is not allowed to insert;

But with Java version less than 1.6, only as 1st element allowed

14-Set-interace-in-java

 

Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

 

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/class-use/NavigableSet.html
http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
https://docs.oracle.com/cd/E21764_01/apirefs.1111/e17503/oracle/adfnmc/java/util/TreeSet.html
https://docs.oracle.com/cd/E21764_01/apirefs.1111/e17503/index.html?oracle/adfnmc/java/util/TreeSet.html

 

Happy Coding !!
Happy Learning !!