Wednesday 30 April 2014

Hashset vs Treeset

  1. HashSet offers constant time performance for the basic operations (add, remove, contains and size). TreeSet guarantees log(n) time cost for the basic operations (add, remove and contains)
  1. HashSet does not guarantee that the order of elements will remain constant over time. TreeSet guarantees that elements of set will be sorted (ascending, natural, or by the Comparator used)
  1. HashSet iteration performance depends on the initial capacity and the load factor of the HashSet. TreeSet iteration performance depends on the height of the tree.
  1. HashSet allows null element, where as treeSet not.
  1. HashSet implemented using hash table, where as TreeSet implemented using a tree structure like red-black tree.
  1. HashSet uses equals method in java for comparison, where as TreeSet uses compareTo method.
  1. TreeSet provides methods like floor, ceil to get the near values and subSet method to get a sub set of elements, where as HashSet not.
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment