Difference between HashSet and TreeSet

Difference between HashSet and TreeSet

Answer: Below are difference between HashSet and TreeSet:

HashSetTreeSet
HashSet elements order is not constant overtimeTreeSet 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 performanceTreeSet guarantees log(n) time cost for (remove, add and contains) basic operations.
When you iterate its performance is depends on load factor or initial capacityThis class don’t have any kind of tuning parameters available for iterations performance
HashSet class has been implemented using hash tableTreeSet class has been implemented using tree structure
Because this class provide no ordering so there are no method available to deal with orderingThis 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 *