HashSet

  • The underlying data structure for HashSet is HashTable
  • Duplicates not allowed
  • Insertion order is not preserved
  • All objects inserted based on hashcode
  • Null insertion is possible
  • Heterogeneous objects are allowed
  • Implements Cloneable and Serializable
  • Best Case for Search

Methods :

  • boolean add(Object o)
    In Hash set duplicates are not allowed, if we do add duplicates then add method returns false.

Initial Capacity

  • The initial bucket size of the collection is called Initial capacity

Load Factor / Fill Ratio

  • In normal collection, if we completely fill the initial capacity then only it increases the capacity but in Hashing related concept if the Load Factor is breached then the collection starts expanding itself to its next capacity.
  • Example : If load factor is 0.75 then after filling 75% of the capacity a new Hash Size is increased

Constructers :

  • HashSet h = new HashSet();
    • Default initial capacity is 16
    • Default fill ratio is 0.75 //Load Factor
  • HashSet h = new HashSet(int initialCapacity)
    • Initial Capacit is passed
    • Default fill ratio is 0.75 //Load Factor
  • HashSet h = new HashSet(int initialCapacity, float fillRatio)
    • Initial Capacity nad Fill Ratio both are passed
  • HashSet h = new HashSet(Collection c)
    • Intercollection conversion

Example :

Leave a Comment