Differences between HashSet and LinkedHashSet and TreeSet in java
Difference Between HashSet, LinkedHashSet And TreeSet
HashSet, LinkedHashSet and TreeSet are the implementations of Set interface which does not allow duplicate elements. In this tutorial we will see the differences between them.
Duplicate Elements
- All the Set implementations does not allow duplicate elements
Order of Elements
- HashSet does not guarantee any order
- LinkedHashSet maintain insertion order
- TreeSet maintain sorting order
Null Elements
- HashSet and LinkedHashSet allows only one null
- TreeSet does not allow null
Thread-Safe
- All three are not thread-safe
Iterator
- Fail-Fast Iterator is returned by HashSet, LinkedHashSet and TreeSet.
Check for Duplicate Elements
- HashSet and LinkedHashSet uses equals() method
- TreeSet uses compareTo() method
post a comment