What are the Situations in Which You Choose HashSet or TreeSet in Java ?

HashSet is better than TressSet in almost every way. It gives O(1) for add(), remove() and contains() operations. Whereas, TressSet gives O(log(N)) for these operations.

Still, TreeSet is useful when you wish to maintain order over the inserted elements or query for a range of elements within the set.

We should use TreeSet when we want to maintain order. Or when there are enough read operations to offset the increased cost of write operations.



You May Interest

What are the Different Types of Events Provided by Spring Framewo ...

Java How To Find The Average Of 10 Numbers Using A While Loop

What are the Differences Between Comparable and Comparator in Jav ...

How Will You Create a Shallow Copy of a Map in Java ?

When Does JVM Call the Finalize Method in Java ?