Home Java Collections Methods singleton() Java Collections singleton() Method 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.
Set<String> admins = Collections.singleton("root"); // [root]
list.removeAll(Collections.singleton("spam")); // remove every "spam"
list.retainAll(Collections.singleton("keep")); // keep only "keep" Call What 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.util.Collections
public static <T> Set<T> singleton(T o) Parameters Parameter Description oThe only element of the set. May be null.
Return Value Return Value Description Set<T>An immutable set of size 1 containing o.
Important
The set is immutable: add, clear, and removing its element throw UnsupportedOperationException. Unlike Set.of, it accepts null, so singleton(null) is a valid one-element set.
Compare with Similar Methods Single-element and small fixed collections:
Java Examples 1. Remove every copy 2. Immutable and null 3. retainAll and seeding Example 1 — Remove Every Copy of a Value remove("a") deletes only the first "a"; removeAll(singleton("a")) deletes them all.
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);
}
} remove(a): [b, a, c, a]
removeAll(singleton(a)): [b, c] How It Works remove(Object). List.remove stops after the first match, so two "a" values remain.Wrap the value. removeAll needs a collection, and singleton("a") is the cheapest collection that holds just "a".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.
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());
}
} [admin] size 1 admin? true
add: UnsupportedOperationException
singleton(null): [null] size 1 How It Works Exactly one. The set prints as [admin], has size 1, and contains "admin".Cannot grow. Adding "guest" would make two elements, so add throws UnsupportedOperationException.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.
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);
}
} only red: [red, red]
seeded set: [2, 5] How It Works Keep, not remove. retainAll keeps elements that are in the given collection, so both "red" entries survive and the rest go.Seeding. The TreeSet copy constructor takes the one element 5 from the singleton.Copy is changeable. The TreeSet is a normal set, so adding 2 works and the elements print in sorted order.Frequently Asked Questions What does Collections.singleton() return? An immutable Set that contains exactly the one element you pass.
How do I remove all occurrences of a value from a list? Call list.removeAll(Collections.singleton(value)). list.remove(value) removes only the first occurrence.
Can I add elements to a singleton set? No. It is immutable, so add throws UnsupportedOperationException.
Can Collections.singleton() hold null? Yes. singleton(null) is a valid set of size 1. Set.of(null) in Java 9+ would throw NullPointerException.
What is the difference between singleton() and singletonList()? singleton returns a Set; singletonList returns a List. Both are immutable and hold one element.
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