HashSets

You’ve just learned that a HashMap stores key–value pairs, with each key being unique.

A HashSet is similar, except it stores only the values, and each value must be unique.
There are no keys — you just add elements, and the set makes sure no duplicates are allowed.

Like HashMap, HashSet is contained in java.util.

General Syntax for Creating a HashSet

import java.util.HashSet;

HashSet<ElementType> name = new HashSet<ElementType>();

To add items to a HashSet, use the method .add(E element) where E is the element type parameter.

Here is an example of a set that stores String names:

public class Program {
    public static void main(String[] args) {

        HashSet<String> names = new HashSet<String>();

        names.add("Johnny");
        names.add("Carl");
        names.add("Grandpa");
        names.add("Carl"); // duplicate - ignored

        System.out.println(names);
        // prints something like: [Grandpa, Johnny, Carl]
        // order is not guaranteed
    }
}

More Methods

  • To check if the set contains an element, use .contains(E element):

    names.contains("Carl"); // true
    names.contains("Billy"); // false
    
  • To remove an element, use .remove(E element):

    names.remove("Johnny"); // Johnny is no longer in the set
    
  • To get the number of items, use .size():

    names.size();
    
  • To remove all elements, use .clear():

    names.clear();
    

For more information, check out w3schools


Task

Create a set of your favorite foods (Strings). Print the set, check if a certain food is in it, and remove one.