Search

Thursday, July 30, 2015

HashSet vs TreeSet in Java? Similarities and Differences

HashSet and TreeSet both implement same interface i.e  java.util.Set interface and they posses the quality of Set interface means duplicate elements are not allowed. Both HashSet and TreeSet are used for to store unique elements, but HashSet doesn't care about any order and TreeSet keeps thing in order. Ordering or sorting on TreeSet can be customized by using Comparator interface, by default TreeSet uses elements natural order for sorting, which is defined by compareTo() method of java.lang.Comparable interface. What is difference between HashSet and TreeSet is is also one the frequently asked Java interview question, So you should know about similarities and difference between them. It also helps you to understand when to use both TreeSet and HashSet and whats are the scenario when we should use this sets. Let's go through the similarities and difference between HashSet and TreeSet in Java.
Read more »

No comments:

Post a Comment