Total WebSite Views Count

HashSet Vs LinkedHashSet Vs TreeSet In Java

HashSet Vs LinkedHashSet Vs TreeSet In Java

HashSet Vs TreeSet Vs LinkedHashSet In Java :

Even though, HashSet, LinkedHashSet and TreeSet are all implementations of Set interface, there are some differences exist between them.

Differences Between HashSet, LinkedHashSet and TreeSet In Java :


HashSetLinkedHashSetTreeSet
How they work internally?HashSet uses HashMap internally to store it’s elements.LinkedHashSet uses LinkedHashMap internally to store it’s elements.TreeSet uses TreeMap internally to store it’s elements.
Order Of ElementsHashSet doesn’t maintain any order of elements.LinkedHashSet maintains insertion order of elements. i.e elements are placed as they are inserted.TreeSet orders the elements according to supplied Comparator. If no comparator is supplied, elements will be placed in their natural ascending order.
PerformanceHashSet gives better performance than the LinkedHashSet and TreeSet.The performance of LinkedHashSet is between HashSet and TreeSet. It’s performance is almost similar to HashSet. But slightly in the slower side as it also maintains LinkedList internally to maintain the insertion order of elements.TreeSet gives less performance than the HashSet and LinkedHashSet as it has to sort the elements after each insertion and removal operations.
Insertion, Removal And Retrieval OperationsHashSet gives performance of order O(1) for insertion, removal and retrieval operations.LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations.TreeSet gives performance of order O(log(n)) for insertion, removal and retrieval operations.
How they compare the elements?HashSet uses equals() and hashCode() methods to compare the elements and thus removing the possible duplicate elements.LinkedHashSet also uses equals() and hashCode() methods to compare the elements.TreeSet uses compare() or compareTo() methods to compare the elements and thus removing the possible duplicate elements. It doesn’t use equals() and hashCode() methods for comparision of elements.
Null elementsHashSet allows maximum one null element.LinkedHashSet also allows maximum one null element.TreeSet doesn’t allow even a single null element. If you try to insert null element into TreeSet, it throws NullPointerException.
Memory OccupationHashSet requires less memory than LinkedHashSet and TreeSet as it uses only HashMap internally to store its elements.LinkedHashSet requires more memory than HashSet as it also maintains LinkedList along with HashMap to store its elements.TreeSet also requires more memory than HashSet as it also maintains Comparator to sort the elements along with the TreeMap.
When To Use?Use HashSet if you don’t want to maintain any order of elements.Use LinkedHashSet if you want to maintain insertion order of elements.Use TreeSet if you want to sort the elements according to some Comparator.

Similarities Between HashSet, LinkedHashSet and TreeSet In Java :
  • All three doesn’t allow duplicate elements.
  • All three are not synchronized.
  • All three are Cloneable and Serializable.
  • Iterator returned by all three is fail-fast in nature. i.e You will get ConcurrentModificationException if they are modified after the creation of Iterator object.

AWS Services

AWS Services

Technology Selection & Evaluation Criteria

Technology Selection & Evaluation Criteria

Scale Cube - Scale In X Y Z Cube

Scale Cube - Scale In X Y Z Cube

Feature Post

AWS Services

About Me

About Me

Spring Cloud

Spring Cloud
Spring Cloud

Spring Cloud +mCloud Native + Big Data Archittect

Spring Cloud +mCloud Native + Big Data Archittect

ACID Transaction

ACID Transaction

Data Pipe Line Stack

Data Pipe Line Stack

Popular Posts