Tuesday, 5 August 2014

Defference between HashSet and TreeSet


 HashSet
  1. The underlying Data Structure for HashSet is Hashtable.
  2.  Insertion order is not preserved and it is based on has code of the Object.
  3.  Duplicate objects are not allowed. Violation leads to no Compile Time Error or RuntimeError, add  method simply returns false. 
  4.  ‘null’ insertion is possible.
  5.  Heterogeneous objects are allowed.
  6.  HashSet is the best choice if the frequent operation is Search Operation.
TreeSet
  1. The underlying Data structure for the TreeSet is Balanced tree.
  2.  Duplicate objects are not allowed. If we are trying to insert duplicate object we won’t get any compile time error or Run time error, add method simply returns false.
  3.  Insertion order is not preserved but all the elements are inserted according to some sorting order. 
  4.  ‘null’ insertion is not possible.
  5.  Heterogeneous objects are not allowed, violation leads to Run time error saying class cast Exception.

No comments:

Post a Comment