Home Java Collections Methods emptySet() Java Collections emptySet() Method Definition In Java, Collections.emptySet() returns a Set that contains no elements and cannot be changed. Use it wherever a method or map value should be "a set of nothing" rather than null.
Like the other empty collections, it is immutable and can be shared freely. It behaves like any other empty set: contains is always false, it equals every other empty set, and iterating over it does nothing.
A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.emptySet(...), after import java.util.Collections;. You never create a Collections object.
Syntax Call it with no arguments; the element type is inferred.
Set<String> tags = Collections.emptySet(); // []
boolean has = tags.contains("java"); // false
Set<String> fallback = map.getOrDefault(key, Collections.emptySet()); Call What it does Collections.emptySet()Shared immutable empty set. map.getOrDefault(k, Collections.emptySet())Safe default when a key is missing. new TreeSet<>()An empty set you can add to. Set.of()Java 9+ immutable empty set.
There is also a raw constant, Collections.EMPTY_SET, from Java 1.2. The generic method emptySet() (Java 5) avoids unchecked warnings.
Library declarations // java.util.Collections
public static final <T> Set<T> emptySet()
public static final Set EMPTY_SET // raw constant, older style Parameters Parameter Description None emptySet() takes no parameters.
Return Value Return Value Description Set<T>An empty, immutable set.
Important
add, addAll with elements, and other changes throw UnsupportedOperationException. If the code that receives the set must add to it, give it a new TreeSet or LinkedHashSet instead.
Compare with Similar Methods Related set factories:
Java Examples 1. Safe default 2. Set logic 3. Map default Example 1 — Tags for a Post That Has None A method returns the tags of a post, or an empty set when the post has none.
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class CollectionsEmptySetTags {
static Set<String> tagsOf(int postId) {
if (postId == 1) {
return new TreeSet<>(Arrays.asList("java", "tips"));
}
return Collections.emptySet();
}
public static void main(String[] args) {
for (int id = 1; id <= 2; id++) {
Set<String> tags = tagsOf(id);
System.out.println("post " + id + " " + tags + " java? " + tags.contains("java"));
}
}
} post 1 [java, tips] java? true
post 2 [] java? false How It Works Real tags. Post 1 gets a TreeSet, so its tags print in alphabetical order.No tags. Post 2 gets Collections.emptySet(), which prints as [].Same code path. contains("java") works on both results, returning false for the empty set instead of crashing.Example 2 — containsAll and add on an Empty Set Every set "contains all" of an empty set, but an empty set does not contain all of a non-empty one. Adding to it fails.
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class CollectionsEmptySetLogic {
public static void main(String[] args) {
Set<String> skills = new TreeSet<>(Arrays.asList("java", "sql"));
Set<String> none = Collections.emptySet();
System.out.println("skills containsAll none: " + skills.containsAll(none));
System.out.println("none containsAll skills: " + none.containsAll(skills));
try {
none.add("go");
} catch (UnsupportedOperationException e) {
System.out.println("add: " + e.getClass().getSimpleName());
}
}
} skills containsAll none: true
none containsAll skills: false
add: UnsupportedOperationException How It Works Nothing to miss. An empty set has no element that skills could lack, so containsAll is true.The other way round. The empty set does not contain "java" or "sql", so the reverse check is false.Immutable. Trying to add "go" throws UnsupportedOperationException.Example 3 — A Default Value in a Map of Sets Look up friends in a map; a missing person gets an empty set, which equals any other empty set.
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class CollectionsEmptySetDefault {
public static void main(String[] args) {
Map<String, Set<String>> friends = new TreeMap<>();
friends.put("ana", new TreeSet<>(Arrays.asList("ben", "cy")));
Set<String> zed = friends.getOrDefault("zed", Collections.emptySet());
System.out.println("zed has " + zed.size() + " friends " + zed);
System.out.println("equals empty TreeSet: " + zed.equals(new TreeSet<String>()));
System.out.println("map unchanged: " + friends);
}
} zed has 0 friends []
equals empty TreeSet: true
map unchanged: {ana=[ben, cy]} How It Works Missing key. "zed" is not in the map, so getOrDefault returns the empty set passed as the default.Set equality. Two sets are equal when they hold the same elements, so the empty set equals an empty TreeSet.No side effect. Unlike computeIfAbsent, getOrDefault does not store anything, so the map still holds only ana.Frequently Asked Questions What does Collections.emptySet() return? An empty, immutable Set. It never contains anything and cannot be changed.
Can I add to the set returned by emptySet()? No. add throws UnsupportedOperationException. Create a TreeSet, LinkedHashSet, or HashSet if you need to add elements.
Is Collections.emptySet() equal to new HashSet<>()? Yes. Set equality compares contents, and both are empty.
Why use emptySet() instead of returning null? Callers can call contains, size, or loop over the result without checking for null first.
What is the difference between emptySet() and Set.of()? Both are immutable and empty. Set.of() was added in Java 9; emptySet() works since Java 5.
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.
10 people found this page helpful
Helpful Share Copy link Suggestion