- Speed and internal implementation
HashSet : For operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table.
TreeSet : TreeSet takes O(Log n) for search, insert and delete which is higher than HashSet. But TreeSet keeps sorted data. Also, it supports operations like higher() (Returns least higher element), floor(), ceiling(), etc. These operations are also O(Log n) in TreeSet and not supported in HashSet. TreeSet is implemented using a Self Balancing Binary Search Tree (Red-Black Tree). TreeSet is backed by TreeMap in Java.
Elements in HashSet are not ordered. TreeSet maintains objects in Sorted order defined by either Comparable or Comparator method in Java. TreeSet elements are sorted in ascending order by default. It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc.
HashSet allows null object. TreeSet doesn’t allow null Object and throw NullPointerException, Why, because TreeSet uses compareTo() method to compare keys and compareTo() will throw java.lang.NullPointerException.
HashSet uses equals() method to compare two object in Set and for detecting duplicates. TreeSet uses compareTo() method for same purpose.
If equals() and compareTo() are not consistent, i.e. for two equal object equals should return true while compareTo() should return zero, than it will break contract of Set interface and will allow duplicates in Set implementations like TreeSet
If you want a sorted Set then it is better to add elements to HashSet and then convert it into TreeSet rather than creating a TreeSet and adding elements to it.
HashSet contains: practice geeks for contribute
TreeSet contains: contribute for geeks practice
When to prefer TreeSet over HashSet
1. Sorted unique elements are required instead of unique elements. The sorted list given by TreeSet is always in ascending order.
2. TreeSet has greater locality than HashSet.If two entries are near by in the order, then TreeSet places them near each other in data structure and hence in memory, while HashSet spreads the entries all over memory regardless of the keys they are associated to.
3. TreeSet uses Red- Black tree algorithm underneath to sort out the elements. When one need to perform read/write operations frequently, then TreeSet is a good choice.
4. LinkedHashSet is another data structure that is between these two. It provides time complexities like HashSet and maintains order of insertion (Note that this is not sorted order, but the order in which elements are inserted).
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Convert HashSet to TreeSet in Java
- HashSet in Java
- Internal working of Set/HashSet in Java
- Initializing HashSet in Java
- HashSet contains() Method in Java
- HashSet clear() Method in Java
- HashSet isEmpty() Method in Java
- HashSet iterator() Method in Java
- HashSet remove() Method in Java
- HashSet size() Method in Java
- HashSet add() Method in Java
- HashSet clone() Method in Java
- Traverse through a HashSet in Java
- HashSet spliterator() method in Java
- How to sort HashSet in Java
- Difference between ArrayList and HashSet in Java
- HashSet hashCode() method in Java with Example
- HashSet containsAll() method in Java with Example
- HashSet toString() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.