skip to content

What are the Collections.emptyList and singletonList factories, and why are they useful?

level: juniorimportance: should knowfreq 45%

answer

  1. emptyList/Set/Map = immutable zero-element, reused today
  2. singletonList/singleton/singletonMap = immutable one element
  3. return empty over null to skip null checks
  4. all immutable; real changes throw
  5. List.of is the Java 9+ uniform alternative (rejects null)

basics

~20 s

Collections.emptyList() gives you an immutable empty list, which the JDK reuses, and singletonList(x) gives an immutable one-element list. They are handy lightweight return values when you'd otherwise create a tiny list, and they can't be modified.

solid answer

~40 s

Collections.emptyList(), emptySet(), and emptyMap() return immutable, zero-element collections; the current JDK reuses one instance, so repeated calls allocate nothing, though the Javadoc only says implementations need not create a new object per call. Collections.singletonList(x), singleton(x) (a set), and singletonMap(k,v) return immutable collections holding exactly one element, implemented with a compact internal layout. They are useful as cheap return values: returning emptyList() instead of null avoids null checks, and singletonList is a tidy way to pass one item to an API that wants a collection. A call that would change them throws UnsupportedOperationException; a call with nothing to change may not, such as emptyList().clear(). In Java 9+ List.of() and List.of(x) cover the same ground with a more uniform API, so new code often prefers those, but the Collections factories remain widely used and valid.

code

java · 10 lines
java
// No results: return an empty list, not null
List<String> results = found ? hits : Collections.emptyList();

// One item into a List-shaped API
process(Collections.singletonList("only"));

Collections.singletonList("x").add("y"); // throws UnsupportedOperationException

// Java 9+ uniform alternative
List<String> alt = List.of("only");

go deeper

for a junior

Knows emptyList()/singletonList(x) give small immutable collections and that returning empty beats returning null.

for a middle

Knows the empty instances are shared in the current JDK, an implementation note, and that all are immutable; distinguishes the two senses of 'singleton'.

for a senior

Compares with List.of/Map.of (null handling, uniformity) and uses empty returns as an API-design convention.

for a principal

Sets codebase conventions (never return null collections), reasons about allocation/garbage and immutable-value sharing across APIs.

## The factories `Collections` provides tiny pre-built collections: - **Empty:** `emptyList()`, `emptySet()`, `emptyMap()` — collections with **zero** elements (Java 1.5). - **Singleton:** `singletonList(x)` (one-element list, Java 1.3), `singleton(x)` (one-element *set*), `singletonMap(k, v)` (one-entry map, Java 1.3). All of them are **immutable**: a call that would change one (`add`, `put`, `set`, or `remove` of an element that is present) throws `UnsupportedOperationException`. A call with nothing to change need not throw, and here it often does not: `Collections.emptyList().clear()` returns quietly, and `Collections.singletonList("a").remove("zzz")` returns `false`. The `Collection` Javadoc permits that for no-effect calls while recommending against it. ## Why they exist: cheap, intention-revealing values 1. **Avoid returning `null`.** A method that sometimes has no results should return an *empty* collection, not `null`, so callers can loop without a null check. `return Collections.emptyList();` does this with **no allocation** in the current JDK: the method returns the same shared instance every time. The Javadoc states that only as an implementation note (implementations "need not create a separate `List` object for each call"), so don't rely on its identity. Allocating `new ArrayList<>()` each time would create garbage for no reason. 2. **Pass one item to a collection-shaped API.** When a method wants a `List<T>` but you have exactly one element, `Collections.singletonList(x)` is a compact one-liner instead of building an `ArrayList` and adding to it. Because they are immutable and (for empty, in the current JDK) shared, they are both memory-efficient and safe to return without defensive copying — nobody can change which elements they hold. ## Type inference note on emptyList `emptyList()` is generic, and the compiler infers the element type from the target type. An assignment, `List<String> xs = Collections.emptyList();`, has worked since the method arrived in Java 1.5. Java 8 added method arguments (JEP 101), so `process(Collections.emptyList());` compiles too, and a conditional branch works the same way: `found ? hits : Collections.emptyList()`. You still write the type witness, `Collections.<String>emptyList()`, when there is no target: `var xs = Collections.emptyList();` gives a `List<Object>`. ## Relationship to List.of (Java 9+) `List.of()` (empty) and `List.of(x)` (single element) and `Map.of(k,v)` cover the same needs with one uniform, overloaded factory family. Differences worth knowing: - `singletonList` permits a `null` element; `List.of` rejects `null` (throws `NullPointerException`). - `List.of` throws on no-effect calls too: `List.of().clear()` throws where `Collections.emptyList().clear()` does not, since its Javadoc says any mutator "will always" throw. - Both empty factories reuse one instance in the current JDK, and neither promises it: the `List.of` Javadoc says to make no assumptions about identity. New code often standardizes on `List.of` / `Map.of` for consistency, but the `Collections` factories are perfectly valid and ubiquitous in existing code and APIs. ## Key terms - **Factory method:** a static method that produces an object, hiding the construction details. - **Singleton (object sense):** a single shared instance reused everywhere — here, the empty collection the current JDK reuses. - **Singleton (collection sense):** a collection containing exactly one element (the meaning in `singletonList`). - **Immutable:** cannot be changed after creation; a call that would change it throws `UnsupportedOperationException`.

  • Why return Collections.emptyList() instead of null?
    Callers can iterate or stream over it without a null check, eliminating a whole class of NullPointerExceptions; and in the current JDK it allocates nothing, because the method returns a shared instance.
  • What is the Java 9+ equivalent of singletonList(x)?
    List.of(x). It is unmodifiable too, but it rejects null elements, whereas singletonList allows a single null.

  • Returning null instead of emptyList for 'no results'
  • Trying to add to a singletonList
  • Confusing 'singleton' the design pattern with singletonList the one-element collection
  • Assuming singletonList rejects null (it allows null; List.of does not)
  • Assuming every mutator on emptyList throws (clear() quietly does nothing)

Collections.emptyList
Java 1.5
Collections.singletonList
Java 1.3

Assumes JDK 27, the same at JDK 25. emptyList dates from Java 1.5 and singletonList from 1.3; Java 8 added method arguments as a source for the inferred element type, beside assignment.

context