In this article, we will learn, the difference between HashSet vs LinkedHashSet and TreeSet And similarities between LinkedHashSet and TreeSet. HashSet, LinkedHashSet, and TreeSet all implement the Set interface. So we have tried to list out the differences and similarities between HashSet, LinkedHashSet, and TreeSet in java.
Differences Between HashSet, LinkedHashSet, and TreeSet:
Features |
HashSet |
LinkedHashSet |
TreeSet |
---|---|---|---|
Internal Working |
HashSet internally uses HashMap for storing objects |
LinkedHashSet uses LinkedHashMap internally to store objects |
TreeSet uses TreeMap internally to store objects |
When To Use |
If you don’t want to maintain insertion order but want to store unique objects |
If you want to maintain the insertion order of elements then you can use LinkedHashSet |
If you want to sort the elements according to some Comparator then use TreeSet |
Order |
HashSet does not maintain insertion order |
LinkedHashSet maintains the insertion order of objects |
While TreeSet orders the elements according to supplied Comparator. By default, objects will be placed according to their natural ascending order. |
Complexity of Operations |
HashSet gives O(1) complexity for insertion, removing, and retrieving objects |
LinkedHashSet gives insertion, removing, and retrieving operations performance in order O(1). |
While TreeSet gives the performance of order O(log(n)) for insertion, removing, and retrieving operations. |
Performance |
The performance of HashSet is better when compared to LinkedHashSet and TreeSet. |
The performance of LinkedHashSet is slower than TreeSet. It is almost similar to HashSet but slower because LinkedHashSet internally maintains LinkedList to maintain the insertion order of elements |
TreeSet performance is better than LinkedHashSet except for insertion and removal operations because it has to sort the elements after each insertion and removal operation. |
Compare |
HashSet uses equals() and hashCode() methods to compare the objects |
LinkedHashSet uses equals() and hashCode() methods to compare it’s objects |
TreeSet uses compare() and compareTo() methods to compare the objects |
Null Elements |
HashSet allows only one null value. |
LinkedHashSet allows only one null value. |
TreeSet does not permit null value. If you insert null value into TreeSet, it will throw NullPointerException. |
Syntax |
HashSet obj = new HashSet(); |
LinkedHashSet obj = new LinkedHashSet(); |
TreeSet obj = new TreeSet(); |
Differences Between HashSet, LinkedHashSet, and TreeSet According to Insertion Order and Time Taken:
Java
// Java program to demonstrate difference between // HashSet, LinkedHashSet and TreeSet according // to insertion order and insertion time import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.TreeSet; class GFG1 { // Function show insertion order of // LinkedHashSet, TreeSet and HashSet private static void insertionOrder() { LinkedHashSet<String> geekLinkSet = new LinkedHashSet<>(); TreeSet<String> geekTreeSet = new TreeSet<>(); HashSet<String> geekHashSet = new HashSet<String>(); // Add three object in // LinkedHashSet and TreeSet for (String str : Arrays.asList( "Geek2" , "Geek1" , "Geek3" , "Geek1" )) { geekLinkSet.add(str); geekTreeSet.add(str); geekHashSet.add(str); } // should be sorted order HashSet // stores element in sorted order System.out.println( "Insertion Order" + " of objects in HashSet :" + geekHashSet); // insertion order or elements LinkedHashSet // stores elements as insertion System.out.println( "Insertion Order of " + "objects in LinkedHashSet :" + geekLinkSet); // should be sorted order TreeSet // stores element in sorted order System.out.println( "Insertion Order of" + " objects in TreeSet :" + geekTreeSet); } // Function calculate insertion time of // 1000 objects of LinkedHashSet, // TreeSet and HashSet private static void insertionTime() { // HashSet performance Test // inserting 1000 elements HashSet<Integer> numbersHS = new HashSet<>(); long startTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { numbersHS.add(i); } long endTime = System.nanoTime(); System.out.println( "Total time to insert" + " 1000 elements in" + " HashSet in nanoseconds: " + (endTime - startTime)); // LinkedHashSet performance Test // inserting 1000 elements LinkedHashSet<Integer> numbersLLS = new LinkedHashSet<>(); startTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { numbersLLS.add(i); } endTime = System.nanoTime(); System.out.println( "Total time to insert" + " 1000 elements in" + " LinkedHashSet nanoseconds: " + (endTime - startTime)); // TreeSet performance Test inserting 1000 objects TreeSet<Integer> numbersTS = new TreeSet<>(); startTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { numbersTS.add(i); } endTime = System.nanoTime(); System.out.println( "Total time to insert" + " 1000 elements in" + " TreeSet in nanoseconds: " + (endTime - startTime)); } // Function calculate deletion time // of 1000 objects LinkedHashSet, // TreeSet and HashSet // Deletion time always vary private static void deletion() { // HashSet performance Test inserting // and deletion 1000 elements HashSet<Integer> deletionHS = new HashSet<>(); for ( int i = 0 ; i < 1000 ; i++) { deletionHS.add(i); } long startingTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { deletionHS.remove(i); } long endedTime = System.nanoTime(); System.out.println( "Total time to Deletion " + "1000 elements in HashSet in nanoseconds: " + Math.abs(startingTime - endedTime)); // LinkedHashSet performance Test inserting // and deletion 1000 elements LinkedHashSet<Integer> deletionLLS = new LinkedHashSet<>(); for ( int i = 0 ; i < 1000 ; i++) { deletionLLS.add(i); } startingTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { deletionLLS.remove(i); } endedTime = System.nanoTime(); System.out.println( "Total time to Deletion 1000" + " elements in LinkedHashSet in nanoseconds: " + Math.abs(startingTime - endedTime)); // TreeSet performance Test inserting // and deletion 1000 elements TreeSet<Integer> deletionTS = new TreeSet<>(); for ( int i = 0 ; i < 1000 ; i++) { deletionTS.add(i); } startingTime = System.nanoTime(); for ( int i = 0 ; i < 1000 ; i++) { deletionTS.remove(i); } endedTime = System.nanoTime(); System.out.println( "Total time to Deletion 1000" + " elements in TreeSet in nanoseconds: " + Math.abs(startingTime - endedTime)); } public static void main(String args[]) { insertionOrder(); insertionTime(); deletion(); } } |
Insertion Order of objects in HashSet :[Geek3, Geek2, Geek1] Insertion Order of objects in LinkedHashSet :[Geek2, Geek1, Geek3] Insertion Order of objects in TreeSet :[Geek1, Geek2, Geek3] Total time to insert 1000 elements in HashSet in nanoseconds: 791869 Total time to insert 1000 elements in LinkedHashSet nanoseconds: 882417 Total time to insert 1000 elements in TreeSet in nanoseconds: 11797657 Total time to Deletion 1000 elements in HashSet in nanoseconds: 834509 Total time to Deletion 1000 elements in LinkedHashSet in nanoseconds: 898922 Total time to Deletion 1000 elements in TreeSet in nanoseconds: 7437577
Similarities Between HashSet, LinkedHashSet, and TreeSet:
- Duplicates: HashSet, LinkedHashSet and TreeSet are implements Set interface, so they are not allowed to store duplicates objects.
- Thread-safe: If we want to use HashSet, LinkedHashSet, and TreeSet in a multi-threading environment then first we make it externally synchronized because both LinkedHashSet and TreeSet are not thread-safe.
- All three are Cloneable and Serializable.
When to use HashSet, TreeSet, and LinkedHashSet in Java:
- HashSet: If you don’t want to maintain insertion order but want to store unique objects.
- LinkedHashSet: If you want to maintain the insertion order of elements then you can use LinkedHashSet.
- TreeSet: If you want to sort the elements according to some Comparator then use TreeSet.
So as you see the output of the above program according to that and according to your requirements, you can choose anyone from HashSet, TreeSet, and LinkedHashSet.