Java Collections emptySet() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

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.

Java
Set<String> tags = Collections.emptySet();      // []
boolean has = tags.contains("java");             // false
Set<String> fallback = map.getOrDefault(key, Collections.emptySet());
CallWhat 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
// java.util.Collections
public static final <T> Set<T> emptySet()

public static final Set EMPTY_SET   // raw constant, older style

Parameters

ParameterDescription
NoneemptySet() takes no parameters.

Return Value

Return ValueDescription
Set<T>An empty, immutable set.

Compare with Similar Methods

Related set factories:

MethodHow it differs
singleton(x)Immutable set with exactly one element.
emptySortedSet()Empty set that also offers SortedSet methods.
unmodifiableSet(s)Read-only view of an existing set.
emptyList()The same idea for lists.

Java Examples

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.

Java
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"));
        }
    }
}

How It Works

  1. Real tags. Post 1 gets a TreeSet, so its tags print in alphabetical order.
  2. No tags. Post 2 gets Collections.emptySet(), which prints as [].
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Nothing to miss. An empty set has no element that skills could lack, so containsAll is true.
  2. The other way round. The empty set does not contain "java" or "sql", so the reverse check is false.
  3. 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.

Java
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);
    }
}

How It Works

  1. Missing key. "zed" is not in the map, so getOrDefault returns the empty set passed as the default.
  2. Set equality. Two sets are equal when they hold the same elements, so the empty set equals an empty TreeSet.
  3. No side effect. Unlike computeIfAbsent, getOrDefault does not store anything, so the map still holds only ana.

Frequently Asked Questions

An empty, immutable Set. It never contains anything and cannot be changed.
No. add throws UnsupportedOperationException. Create a TreeSet, LinkedHashSet, or HashSet if you need to add elements.
Yes. Set equality compares contents, and both are empty.
Callers can call contains, size, or loop over the result without checking for null first.
Both are immutable and empty. Set.of() was added in Java 9; emptySet() works since Java 5.

Next: emptySortedMap()

Get an immutable empty SortedMap (Java 8+).

emptySortedMap() 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.

10 people found this page helpful