Java Collections singleton() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.singleton(o) returns a Set that contains exactly one element, o, and can never be changed. It is small and fast, because it stores just that one reference.

Its best-known use is as an argument to methods that expect a collection: list.removeAll(Collections.singleton(x)) deletes every copy of x from a list, while list.remove(x) deletes only the first.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.singleton(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass the single element.

Java
Set<String> admins = Collections.singleton("root");     // [root]
list.removeAll(Collections.singleton("spam"));            // remove every "spam"
list.retainAll(Collections.singleton("keep"));            // keep only "keep"
CallWhat it does
Collections.singleton(x)Immutable set containing only x.
list.removeAll(Collections.singleton(x))Removes all elements equal to x.
list.retainAll(Collections.singleton(x))Removes everything except x.
new TreeSet<>(Collections.singleton(x))A changeable set that starts with x.

singleton is the oldest of the single-element factories (Java 1.2). singletonList and singletonMap followed in Java 1.3. In Java 9+, Set.of(x) is similar but rejects null.

Library declarations
Java
// java.util.Collections
public static <T> Set<T> singleton(T o)

Parameters

ParameterDescription
oThe only element of the set. May be null.

Return Value

Return ValueDescription
Set<T>An immutable set of size 1 containing o.

Compare with Similar Methods

Single-element and small fixed collections:

MethodHow it differs
singletonList(x)One-element immutable List.
singletonMap(k, v)One-entry immutable Map.
emptySet()Immutable set with no elements.
Set.of(x)Java 9+ alternative; throws for null.

Java Examples

Example 1 — Remove Every Copy of a Value

remove("a") deletes only the first "a"; removeAll(singleton("a")) deletes them all.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsSingletonRemoveAll {
    public static void main(String[] args) {
        List<String> first = new ArrayList<>(Arrays.asList("a", "b", "a", "c", "a"));
        first.remove("a");
        System.out.println("remove(a):               " + first);

        List<String> all = new ArrayList<>(Arrays.asList("a", "b", "a", "c", "a"));
        all.removeAll(Collections.singleton("a"));
        System.out.println("removeAll(singleton(a)): " + all);
    }
}

How It Works

  1. remove(Object). List.remove stops after the first match, so two "a" values remain.
  2. Wrap the value. removeAll needs a collection, and singleton("a") is the cheapest collection that holds just "a".
  3. Every match goes. removeAll drops each element contained in the singleton set, leaving only b and c.

Example 2 — One Element, No Changes, null Allowed

Inspect a singleton set, try to add to it, and create one that holds null.

Java
import java.util.Collections;
import java.util.Set;

public class CollectionsSingletonRules {
    public static void main(String[] args) {
        Set<String> roles = Collections.singleton("admin");
        System.out.println(roles + " size " + roles.size() + " admin? " + roles.contains("admin"));

        try {
            roles.add("guest");
        } catch (UnsupportedOperationException e) {
            System.out.println("add: " + e.getClass().getSimpleName());
        }

        Set<String> nothing = Collections.singleton(null);
        System.out.println("singleton(null): " + nothing + " size " + nothing.size());
    }
}

How It Works

  1. Exactly one. The set prints as [admin], has size 1, and contains "admin".
  2. Cannot grow. Adding "guest" would make two elements, so add throws UnsupportedOperationException.
  3. null is an element. singleton(null) is a set with one element that happens to be null; its size is still 1.

Example 3 — Keep Only One Value, and Seed a Changeable Set

Use a singleton to filter a list down to one color, and as the starting content of a TreeSet.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class CollectionsSingletonRetain {
    public static void main(String[] args) {
        List<String> colors = new ArrayList<>(Arrays.asList("red", "blue", "red", "green"));
        colors.retainAll(Collections.singleton("red"));
        System.out.println("only red: " + colors);

        Set<Integer> ids = new TreeSet<>(Collections.singleton(5));
        ids.add(2);
        System.out.println("seeded set: " + ids);
    }
}

How It Works

  1. Keep, not remove. retainAll keeps elements that are in the given collection, so both "red" entries survive and the rest go.
  2. Seeding. The TreeSet copy constructor takes the one element 5 from the singleton.
  3. Copy is changeable. The TreeSet is a normal set, so adding 2 works and the elements print in sorted order.

Frequently Asked Questions

An immutable Set that contains exactly the one element you pass.
Call list.removeAll(Collections.singleton(value)). list.remove(value) removes only the first occurrence.
No. It is immutable, so add throws UnsupportedOperationException.
Yes. singleton(null) is a valid set of size 1. Set.of(null) in Java 9+ would throw NullPointerException.
singleton returns a Set; singletonList returns a List. Both are immutable and hold one element.

Next: singletonList()

Create an immutable List holding exactly one element.

singletonList() 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