Difference between HashSet and TreeSet

Difference between HashSet and TreeSet

Answer: Below are difference between HashSet and TreeSet:

HashSet TreeSet
HashSet elements order is not constant overtime TreeSet guarantees elements will be ordered by their natural ordering or you could specify using its constructor
For basic operations(remove, add, contains and size) this class offers constant performance TreeSet guarantees log(n) time cost for (remove, add and contains) basic operations.
When you iterate its performance is depends on load factor or initial capacity This class don’t have any kind of tuning parameters available for iterations performance
HashSet class has been implemented using hash table TreeSet class has been implemented using tree structure
Because this class provide no ordering so there are no method available to deal with ordering This class provides many operation to deal with ordering i.e. headset(), tailSet(), first(), last() etc…

Leave a Reply

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