Treeset and hashset both belong to set and obey the rule of maintaining uinique elements .
But treeset comes with some more added advantage. TreeSet gives ability to sort elements with the help of the compareTo().
HashSet allows null elements.
Treeset never allows null.
 
Hashset is faster as it is not dealing with sorting of elements.
Treeset has sorting capability so it is somewhat slow.
 
To compare equality hashSet uses equals()
while on the other hand TreeSet uses compareTo() to compare equality and along with that you should nicely implement equals and hashcode if you fail to do so treeset will not recgnize the equal elements.It will end up saving duplicate elements in its collection.
#java #java tutotial for more read.http://amzn.to/2sP654P java concurrency and effective java

Leave a Reply

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