Home Java HashSet Methods isEmpty() Java HashSet isEmpty() Method 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.
Set<String> waiting = new HashSet<>();
boolean a = waiting.isEmpty(); // true
waiting.add("Mia");
boolean b = waiting.isEmpty(); // false Call What 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.util.HashSet<E>
public boolean isEmpty()
// Declared in Set<E> and Collection<E> Parameters Parameter Description None isEmpty() takes no parameters.
Return Value Return Value Description trueThe set has no elements. falseThe set has at least one element, even if that element is null.
Compare with Similar Methods Related checks:
Java Examples 1. Waiting list 2. A set holding null 3. Guarding a null set 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.
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());
}
} new list empty: true
after Mia joins: false
after Mia leaves: true How It Works Born empty. Nothing has been added to the new set, so the first check is true.One element is enough. As soon as Mia is added, the set is no longer empty and the result flips to false.Removal can empty it. Removing the only element returns the set to the empty state, so the last line is true again.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.
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());
}
} isEmpty: false, size: 1
after remove(null): true How It Works null is stored. A HashSet accepts one null element, and it takes up a place like any other value.Not empty. The set has size 1, so isEmpty() is false even though the only element prints as "nothing".Remove it by value. remove(null) deletes that element, which returns the set to size 0.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.
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));
}
} none.isEmpty() -> NullPointerException
no set at all
empty set
tags: [java] How It Works A null variable has no methods. none.isEmpty() fails with NullPointerException because there is no set object to ask.Check null first. describe tests tags == null before it calls isEmpty(), so the missing set is handled safely.Empty is different from missing. The second call receives a real set with no elements, so it reaches the isEmpty() branch.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 How do I check if a HashSet is empty in Java? Call set.isEmpty(). It returns true when the set has no elements.
Is isEmpty() better than size() == 0? They give the same answer. isEmpty() is easier to read, and it is always a constant-time call.
Is a HashSet that contains only null empty? No. null is stored as an element, so the size is 1 and isEmpty() returns false.
Why does isEmpty() throw NullPointerException? The variable itself is null, so there is no set to call the method on. Check set == null before calling isEmpty().
How do I make a HashSet empty again? Call set.clear(), which removes all elements and leaves an empty set you can reuse.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion