Java HashSet

Posted by Hayri Cicek on September 11, 2019


Java HashSet is a collection that contains unique elements and implements the Set interface.
The following example shows how to create a HashSet and add new elements using the add() method.


import java.util.HashSet;

public class HashSetAddElementsExample {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");
        cities.add("Helsingborg");
        
        System.out.println("Cities : " + cities);
    }
}


Output:

$ javac HashSetAddElementsExample.java
$ java HashSetAddElementsExample
Cities : [Göteborg, Malmö, Helsingborg, Stockholm]


In the following example, we will use few HashSet methods to check the size of the HashSet, if it's empty and remove elements.

import java.util.HashSet;

public class HashSetSimpleMethodsExample {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");
        cities.add("Berlin");

        System.out.println("Cities : " + cities);

        // removing "Berlin" from the list
        cities.remove("Berlin");

        // check if the HashSet contains "Helsingborg"
        System.out.println("Contains Helsingborg: " + cities.contains("Helsingborg"));

        // check if HashSet is empty
        System.out.println("Is empty: " + cities.isEmpty());

        // size() method is used to find the size of the HashSet
        System.out.println("Size: " + cities.size());

        // removeAll() will remove everything from the collection
        cities.removeAll(cities);

        System.out.println("Cities: " + cities);
    }
}


Output:

$ javac HashSetSimpleMethodsExample.java
$ tuts java HashSetSimpleMethodsExample
Cities : [Göteborg, Berlin, Malmö, Helsingborg, Stockholm]
Contains Helsingborg: true
Is empty: false
Size: 4
Cities: []


Iterating over a HashSet
The following example shows how to iterate over a HashSet using for-each loop

import java.util.HashSet;

public class IterateHashSetUsingForEach {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        for (String city : cities) { 
            System.out.println("City: " + city);
        }
    }
}


Output:

$ javac IterateHashSetUsingForEach.java
$ java IterateHashSetUsingForEach
City: Göteborg
City: Malmö
City: Helsingborg
City: Stockholm


Iterate over a HashSet using forEach and lambda expression.

import java.util.HashSet;

public class IterateHashSetUsingForEachLambda {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        cities.forEach(city -> {
            System.out.println("City: " + city);
        });
    }
}


Output:

$ javac IterateHashSetUsingForEachLambda.java
$ java IterateHashSetUsingForEachLambda
City: Göteborg
City: Malmö
City: Helsingborg
City: Stockholm


The following example shows how to iterate over a HashSet using iterator() method.

import java.util.HashSet;
import java.util.Iterator;

public class IterateHashSetUsingIteratorMethod {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        Iterator<String> cityItr = cities.iterator();
        while(cityItr.hasNext()) {
            String city = cityItr.next();
            System.out.println("City: " + city);
        }
    }
}


Output:

$ javac IterateHashSetUsingIteratorMethod.java
$ java IterateHashSetUsingIteratorMethod
City: Göteborg
City: Malmö
City: Helsingborg
City: Stockholm


In the following example, we will use iterator() and forEachRemaining() methods to iterate over HashSet elements

import java.util.HashSet;
import java.util.Iterator;

public class ItrHashSetForEachRemainingExample {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();

        cities.add("Helsingborg");
        cities.add("Malmö");
        cities.add("Stockholm");
        cities.add("Göteborg");

        Iterator<String> cityItr = cities.iterator();
        cityItr.forEachRemaining(city -> {
            System.out.println("City: " + city);
        });
    }
}


Output:

$ javac ItrHashSetForEachRemainingExample.java
$ java ItrHashSetForEachRemainingExample
City: Göteborg
City: Malmö
City: Helsingborg
City: Stockholm


Share this: