Difference between HashSet and LinkedHashSet and TreeSet in java

Attribute  HashSet LinkedHashSet TreeSet
Data Structure HashTable.(Internally it’s a HashMap) HashTable + LinkedList Balanced Tree
Element insertion order Not preserved.  Preserved. Not preserved.
Sorting order  No  No  Yes
Null acceptance Allows only one Null. Allows only one Null. We can add only one null element in a TreeSet and you will get NPE if you try to add either null or element after insertion of null.
Duplicate elements Not allowed. Not allowed. Not allowed.
Heterogeneous element insertion  Allowed.  Allowed. Not allowed.

Posted in collections and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">