HashSet class

The HashSet Class HashSet extends AbstractSet and implements the Set interface.

It creates a collection that uses a hash table for storage. HashSet is a generic class that has this declaration:

class HashSet<E>

E specifies the type of objects that the set will hold.

A demonstration of a hashset with String elements unordered

 
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Set<String> ss = new HashSet<String>();
    String[] fruits = { "apples", "pears", "grapes", "bananas", "kiwis", "pears", null };
    for (String fruit : fruits){
      ss.add(fruit);
    }
    for (String s : ss){
      System.out.print(s + " ");
    }
  }
}
  

null grapes bananas kiwis pears apples

A custom class not overriding hashCode()

 
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Set<Item> sp = new HashSet<Item>();
    sp.add(new Item("Q"));
    sp.add(new Item("V"));
    sp.add(new Item("E"));
    sp.add(new Item("M"));
    Item p1 = new Item("5");
    sp.add(p1);
    Item p2 = new Item("b");
    sp.add(p2);
    System.out.println(p1.equals(p2));
    System.out.println(sp);
  }
}

class Item {
  private String name;

  Item(String name) {
    this.name = name;
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Item))
      return false;
    Item p = (Item) o;
    return p.name.equals(name);
  }

  String getName() {
    return name;
  }

  @Override
  public String toString() {
    return name;
  }
}
  

false
[5, V, E, Q, M, b]

A custom class overriding hashCode()

 
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    Set<Item> sp = new HashSet<Item>();
    sp.add(new Item("A"));
    sp.add(new Item("X"));
    sp.add(new Item("E"));
    sp.add(new Item("B"));
    Item p1 = new Item("5");
    sp.add(p1);
    Item p2 = new Item("5");
    sp.add(p2);
    System.out.println(p1.equals(p2));
    System.out.println(sp);
  }
}

class Item {
  private String name;

  Item(String name) {
    this.name = name;
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Item))
      return false;
    Item p = (Item) o;
    return p.name.equals(name);
  }

  String getName() {
    return name;
  }

  @Override
  public int hashCode() {
    return name.hashCode();
  }

  @Override
  public String toString() {
    return name;
  }
}
  

true
[E, A, 5, B, X]

Constructor

HashSet()
Creates a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
HashSet(Collection<? extends E> c)
Creates a new set containing the elements in the specified collection.
HashSet(int initialCapacity)
Creates a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int initialCapacity, float loadFactor)
Creates a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.

HashSet supports the following methods:

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()
Returns true if this set contains no elements.
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).

Revised from Open JDK source code

Home 
  Java Book 
    Collection  

HashSet:
  1. HashSet class
  2. Add element to hash set
  3. Clear a hash set
  4. Clone a hash set
  5. If a hash set contain a certain element
  6. Is this set containing a certain element
  7. Get an iterator from a hash set
  8. Remove an element from hash set
  9. Get the size of a hash set