Difference between HashSet and TreeSet in Java
There are several differences between an HashSet and a TreeSet is similar to what we discussed as a difference between TreeMap and HashMap. Anyway, Set and Map are two completely different interfaces so we will revisit those differences here. Probably the most important difference between HashSet and TreeSet is the performance. HashSet is faster than TreeSet which means if you need performance use HashSet but HashSet doesn't provide any kind of ordering so if you need ordering then you need to switch to TreeSet which provides sorting of keys. Sorting can be natural order defined by a Comparable interface or any particular order defined by Comparator interface in Java.
