- 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)
- 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)
- 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.
- HashSet allows null element, where as treeSet not.
- HashSet implemented using hash table, where as TreeSet implemented using a tree structure like red-black tree.
- HashSet uses equals method in java for comparison, where as TreeSet uses compareTo method.
- 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.
No comments:
Post a Comment