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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.