Differences and Similarities between HashSet, LinkedHashSet and TreeSet in java


In this Collection framework tutorial we will learn what are differences and similarities between java.util.HashSet, java.util.LinkedHashSet and java.util.TreeSet in java.



Differences between java.util.HashSet vs java.util.LinkedHashSet vs java.util.TreeSet in java>


Property
java.util.HashSet
java.util.LinkedHashSet
java.util.TreeSet
1
Insertion order
java.util.HashSet does not maintains insertion order in java.

Example in java >
set.add("b");
set.add("c");
set.add("a");

Output >     
No specific order
java.util.LinkedHashSet maintains insertion order in java.

Example in java >
set.add("b");
set.add("c");
set.add("a");

Output >     
b
c
a
java.util.TreeSet is sorted by natural order in java.

Example in java >
set.add("b");
set.add("c");
set.add("a");

Output >     
a
b
c
2
Null elements
HashSet allows to store one null in java.
LinkedHashSet allows to store one null in java.
TreeSet does not allows to store any null in java.

Any attempt to add null throws runtimeException (NullPointerException).
3
Data structure internally used for storing data
For storing elements HashSet internally uses HashMap.
For storing elements LinkedHashSet internally uses  LinkedHashMap.
For storing elements TreeSet internally uses TreeMap.
4
Introduced  in which java version
java.util.HashSet was introduced in second version of java (1.2) i.e. JDK 2.0
java.util.LinkedHashSet was introduced in second version of java (1.4) i.e. JDK 4.0
java.util.TreeSet was introduced in second version of java (1.2) i.e. JDK 2.0
5
Implements which interface
HashSet implements java.util.Set interface.
LinkedHashSet implements java.util.Set interface.
TreeSet implements java.util.Set
java.util.SortedSet
java.util.NavigableSet interface.

So far we have learned what are differences between java.util.HashSet, java.util.LinkedHashSet and java.util.TreeSet in java. Now we will learn similarities between java.util.HashSet and java.util.LinkedHashSet and java.util.TreeSet in Collection framework in java.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

Similarity in java.util.HashSet vs java.util.LinkedHashSet vs java.util.TreeSet >

Property
java.util.HashSet vs
java.util.LinkedHashSet vs
java.util.TreeSet
1
Iterator
iterators returned by HashSet, LinkedHashSet and TreeSet all of them are fail-fast in java.
2
Implements
HashSet, LinkedHashSet and TreeSet all implements java.util.Set interface in java.
3
Duplicate elements
HashSet, LinkedHashSet and TreeSet all of them does not allow to store duplicate elements in java.



So in this Collection framework tutorial we learned what are differences and similarities between java.util.HashSet and java.util.LinkedHashSet and java.util.TreeSet in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

List hierarchy tutorial in java - Detailed - java.util.ArrayList, java.util.LinkedList, java.util.vector, java.util.concurrent.CopyOnWriteArrayList classes


Set hierarchy tutorial in java - Detailed - java.util.HashSet, java.util.concurrent.CopyOnWriteArraySet, java.util.LinkedHashSet, java.util.TreeSet, java.util.concurrent.ConcurrentSkipListSet, java.util.EnumSet classes


Important Similarity and Differences >

ArrayList vs LinkedList - Similarity and Differences in java


HashMap and Hashtable - Similarity and Differences in java


Iterator vs ListIterator - Similarity and Differences in java



HashSet vs CopyOnWriteArraySet in java - Similarity and Differences with program


TreeSet vs ConcurrentSkipListSet in java - Similarity and Differences with program


eEdit
Must read for you :