Java HashSet isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashSet

Definition

In Java, isEmpty() returns true when a HashSet contains no elements and false otherwise. It is the same test as size() == 0, with a name that explains itself.

A newly created set is empty, and a set becomes empty again after clear() or after its last element is removed.

An instance method of java.util.HashSet (class since Java 1.2): a set of unique elements kept in a hash table, with no guaranteed order, one null element allowed, and no thread safety.

Syntax

No arguments; returns a boolean.

Java
Set<String> waiting = new HashSet<>();
boolean a = waiting.isEmpty();   // true
waiting.add("Mia");
boolean b = waiting.isEmpty();   // false
CallWhat it does
set.isEmpty()true if there are no elements.
!set.isEmpty()true if there is at least one element.
set == null || set.isEmpty()Guard against a missing set and an empty one.
set.size() == 0Equivalent, but longer.

A set that holds only null is not empty, because null is stored as an element.

Library declarations
Java
// java.util.HashSet<E>
public boolean isEmpty()

// Declared in Set<E> and Collection<E>

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
trueThe set has no elements.
falseThe set has at least one element, even if that element is null.

Compare with Similar Methods

Related checks:

MethodHow it differs
size()Gives the exact count when you need more than a yes/no.
clear()Makes any set empty.
contains(e)Checks for one specific element instead.
Collections.emptySet()An immutable set that is always empty.

Java Examples

Example 1 — Check a Waiting List That Fills and Empties

Test a new set, a set with one person, and the set after that person leaves.

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

public class HashSetIsEmptyDemo {
    public static void main(String[] args) {
        Set<String> waiting = new HashSet<>();
        System.out.println("new list empty: " + waiting.isEmpty());

        waiting.add("Mia");
        System.out.println("after Mia joins: " + waiting.isEmpty());

        waiting.remove("Mia");
        System.out.println("after Mia leaves: " + waiting.isEmpty());
    }
}

How It Works

  1. Born empty. Nothing has been added to the new set, so the first check is true.
  2. One element is enough. As soon as Mia is added, the set is no longer empty and the result flips to false.
  3. Removal can empty it. Removing the only element returns the set to the empty state, so the last line is true again.
  4. Always current. isEmpty() looks at the set's present state each time you call it; nothing is cached.

Example 2 — A Set That Holds Only null Is Not Empty

Add null, check the set, and then remove it.

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

public class HashSetIsEmptyNull {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add(null);
        System.out.println("isEmpty: " + set.isEmpty() + ", size: " + set.size());

        set.remove(null);
        System.out.println("after remove(null): " + set.isEmpty());
    }
}

How It Works

  1. null is stored. A HashSet accepts one null element, and it takes up a place like any other value.
  2. Not empty. The set has size 1, so isEmpty() is false even though the only element prints as "nothing".
  3. Remove it by value. remove(null) deletes that element, which returns the set to size 0.
  4. Empty again. With no elements left, the second check is true.

Example 3 — Guard Against Missing and Empty Sets

Describe a missing set, an empty set, and a set with one tag, and see what a null variable does.

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

public class HashSetIsEmptyGuard {
    static String describe(Set<String> tags) {
        if (tags == null) {
            return "no set at all";
        }
        if (tags.isEmpty()) {
            return "empty set";
        }
        return "tags: " + tags;
    }

    public static void main(String[] args) {
        Set<String> none = null;
        Set<String> empty = new HashSet<>();
        Set<String> one = new HashSet<>();
        one.add("java");

        try {
            none.isEmpty();
        } catch (NullPointerException e) {
            System.out.println("none.isEmpty() -> NullPointerException");
        }

        System.out.println(describe(none));
        System.out.println(describe(empty));
        System.out.println(describe(one));
    }
}

How It Works

  1. A null variable has no methods. none.isEmpty() fails with NullPointerException because there is no set object to ask.
  2. Check null first. describe tests tags == null before it calls isEmpty(), so the missing set is handled safely.
  3. Empty is different from missing. The second call receives a real set with no elements, so it reaches the isEmpty() branch.
  4. A single element. One tag means neither condition holds, so the set is printed; a one-element set has only one possible order.

Frequently Asked Questions

Call set.isEmpty(). It returns true when the set has no elements.
They give the same answer. isEmpty() is easier to read, and it is always a constant-time call.
No. null is stored as an element, so the size is 1 and isEmpty() returns false.
The variable itself is null, so there is no set to call the method on. Check set == null before calling isEmpty().
Call set.clear(), which removes all elements and leaves an empty set you can reuse.

Next: iterator()

Walk through the elements one at a time.

iterator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful