All I have been reading lots of blogs on this subject but still I could not get clear idea when to use one over another hashset or tree set.
Taken an example:
I have a comparable objects. I have put them in HashSet. Now when (only when I want) i want to set to be sorted based on compareTo logic, I can call Collections.sort(object)
whereas Treeset by default always use the compareTo or compare(obj1, obj2) all the time. Hence performance will be hit with TreeSet but output will be same as #1 (Collections.sort).
Is this understanding correct?
HashSet is faster than TreeSet. HashSet is Implemented using a hash table. TreeSet takes O(Log n) for search, insert and delete which is higher than HashSet. But TreeSet keeps sorted data.
1) HashSet gives better performance (faster) than TreeSet for the operations like add, remove, contains, size etc. HashSet offers constant time cost while TreeSet offers log(n) time cost for such operations.
HashSet vs TreeSet:HashSet offers constant time cost while TreeSet offers log(n) time cost for such operations. 2- HashSet does not maintain any order of elements while TreeSet elements are sorted in ascending order by default.
A TreeSet is a set where the elements are sorted. A HashSet is a set where the elements are not sorted or ordered. It is faster than a TreeSet.
HashSet is Implemented using a hash table. Elements are not ordered. The add, remove,
and contains methods have constant time complexity O(1).
TreeSet is implemented using a tree structure(red-black tree in algorithm book). The elements in a set are sorted, but the add, remove, and contains methods has time complexity O(log (n)). It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet()
, etc.
1) First major difference between HashSet
and TreeSet
is performance. HashSet
is faster than TreeSet
and should be preferred choice if sorting of element is not required.
2) Second difference between HashSet
and TreeSet
is that HashSet
allows null object but 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
.
3) Another significant difference between HashSet
and TreeSet
is that , HashSet
is backed by HashMap
while TreeSet
is backed by NavigableMap in Java.
4) One more difference between HashSet
and TreeSet
which is worth remembering is that HashSet uses equals()
method to compare two object in Set and for detecting duplicates while 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
5) Now most important difference between HashSet
and TreeSet
is ordering. HashSet
doesn't guaranteed any order while TreeSet
maintains objects in Sorted order defined by either Comparable
or Comparator
method in Java.
6) TreeSet
does not allow to insert Heterogeneous
objects. It will throw classCastException
at Runtime
if trying to add hetrogeneous objects, whereas HashSet
allows hetrogeneous objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With