HashSet in java

Let’s learn HashSet in java.

HashSet in java

  • Underlying data structure for HashSet is hashtable.
  • HashSet implements Set interface.
  • In HashSet duplicates are not allowed. If any duplicate is inserted, then add() method returns “false” without adding any duplicate object. HashSet allows “null” value only once.
  • All objects are inserted based on hash code of objects.
  • Insertion order is not preserved.
  • In HashSet heterogeneous objects are allowed.
  • HashSet implements Serializable and Cloneable interface.
  • In HashSet, searching objects is easier. Because objects are stored based on hashcode.
  • HashSet do not implement RandomAccess interface.

Let’s see hashset example in java.

import java.util.HashSet;

public class HashSetExample 
{
   public static void main(String[] args) 
   {
      HashSet hs = new HashSet();
      hs.add("Banana");
      hs.add("Orange");
      hs.add("Apple");
      hs.add("Pineapple");
      hs.add(null);
      hs.add(15);
      System.out.println(hs.add("Apple"));
      System.out.println(hs);
   }
}

Output:

false
[null, Apple, Pineapple, Orange, Banana, 15]


Initial capacity : is initial number of buckets that a Hashset object can hold.

Load factor/ fill ratio: is a measure to calculate after how much load a new hashset object is created.

HashSet in java

NOTE: best load factor would be 0.75 with respect to time and space complexity, according to java.


HashSet methods

MethodsDescription
boolean add(E e)adds the specified element to this set if it is not already present.
void clear()removes all of the elements from this set.
Object clone()returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
boolean contains(Object o)returns true if this set contains the specified element.
boolean isEmpty()method used to check whether the set is empty or not. Returns true if this set contains no elements and false for non-empty condition.
Iterator<E> iterator()returns an iterator over the elements in this set.
boolean remove(Object o)removes the specified element from this set if it is present.
int size()returns the number of elements in this set(its cardinality).

Constructors in hashset

HashSet h = new HashSet();

This constructor creates an empty hashset object with default initial capacity 16 and default fill ratio or load factor 0.75.


HashSet h = new HashSet(int initialCapacity);

This constructor creates hashset object with specified initialCapacity with load factor 0.75. Nothing but customized initial capacity and default load factor.


HashSet h = new HashSet(int initialCapacity, float fillRatio);

Here we can customize load factor.


HashSet h = new HashSet(Collection C);

Creates an equivalent HashSet for the given Collection. This constructor is meant for interconversion between Collection objects.


NOTE:

What is fill ratio or load factor in hashset?

It is nothing but after filling 75% ratio (0.75) a new HashSet object will be created automatically. This 75% (0.75) ratio is nothing but fill ratio or load factor.


Reference – docs Oracle, oracle docs