skip to content

Collections Utility & Factories

The Collections helpers and the List.of and Map.of factories, including how an unmodifiable view differs from a collection with no backing list, the question interviewers actually ask.

part ofJavaread it →
on this pageshow

explore

questions

10

What is the java.util.Collections class, and how does it differ from the Collection interface?

level: juniorimportance: must knowfreq 70%

answer

  1. Collection = interface, Collections = static toolbox
  2. Mirror of Arrays for plain arrays
  3. final class, private ctor, all static
  4. sort/reverse/shuffle, unmodifiable/synchronized, empty/singleton

basics

~10 s

Collection (no 's') is the interface that lists and sets implement. Collections (with 's') is a helper class full of static methods like sort, reverse, and emptyList that operate on those collections.

solid answer

~30 s

java.util.Collection is the root interface of the collections hierarchy (List, Set, Queue all extend it). java.util.Collections is a separate utility class that only holds static helper methods and constants; you never instantiate it. Its methods operate on or return collections: sort and binarySearch on lists, reverse and shuffle, unmodifiableList and synchronizedMap wrappers, and factories like emptyList and singletonList. The naming is a classic Java gotcha because the two are unrelated types that differ only by an 's'. Collections sits beside Arrays, which plays the same utility role for plain arrays.

go deeper

for a junior

Knows Collections is a helper class with static methods (sort, reverse) and that it is not the same as the Collection interface.

for a middle

Explains the utility-class pattern (final, private ctor, static), and names the wrapper/factory families alongside the algorithms.

for a senior

Articulates why the class exists historically (no interface default methods pre-Java 8) and contrasts it with Arrays and with Stream-based alternatives.

for a principal

Frames it as a design-pattern example, discusses migration toward List.of/Stream APIs, and the legacy/compat tradeoffs of keeping these wrappers.

## The two confusingly named types Java has two types whose names differ by a single letter, and they do completely different jobs: - **`java.util.Collection`** (singular) is an **interface** — the root of the collections type hierarchy. `List`, `Set`, and `Queue` all extend it; `ArrayList`, `HashSet`, etc. ultimately implement it. An interface defines *what operations a collection supports* (add, remove, size, iterator) without saying how. - **`java.util.Collections`** (plural) is a **utility class** — a `final` class with a `private` constructor, so you can never create an instance of it. It contains only `static` methods (and a few constants). A static method belongs to the class itself, so you call it as `Collections.sort(list)`, not on an object. The relationship is: `Collections` is a toolbox *for* working with `Collection` objects, and with `Map`s, which are not `Collection`s. It is the exact analogue of `java.util.Arrays`, which is the toolbox for plain arrays. ## What lives in Collections The class groups several families of helpers: 1. **Algorithms:** on a `List`, `sort(list)`, `sort(list, comparator)`, `binarySearch(list, key)`, `reverse(list)`, `shuffle(list)`, `swap`, `fill`, `rotate`; on any `Collection`, `max`, `min`, `frequency`. 2. **Read-only (unmodifiable) wrappers:** `unmodifiableList`, `unmodifiableSet`, `unmodifiableMap` — return a view that throws `UnsupportedOperationException` on any mutating call. 3. **Thread-safe (synchronized) wrappers:** `synchronizedList`, `synchronizedSet`, `synchronizedMap` — guard each call with one lock, leaving iterator traversal to the caller. 4. **Empty / singleton factories:** `emptyList()`, `emptySet()`, `emptyMap()`, `singletonList(x)`, `singleton(x)`, `singletonMap(k,v)` — tiny immutable collections. ## Why a separate utility class at all? Before Java 8 introduced default methods, an interface could not carry implemented behavior. So shared algorithms that work on *any* `List` (like sorting) had to live somewhere external — a static utility class was the answer. `Collections` dates from Java 1.2. Java 8 then gave `List` a default `sort` method, and `Collections.sort` now simply calls it. ## Key terms defined - **Static method:** a method called on the class, not an instance. - **Utility class:** a class that exists only to hold static helpers; not meant to be instantiated. - **Interface:** a contract of method signatures a type promises to implement. - **View / wrapper:** an object that delegates to an underlying collection rather than copying it; changes to the original are visible through the view. ## The practical takeaway When you want to *do something to* a list or set — sort it, make it read-only, get an empty one — reach for `Collections.xxx(...)`. When you want to *describe* what a parameter accepts, type it as `Collection<T>` (the interface).

  • Can you instantiate Collections?
    No. It is a final class with a private constructor; every member is static, so you only ever call Collections.xxx(...).
  • What is the array equivalent of Collections?
    java.util.Arrays — it provides Arrays.sort, Arrays.asList, Arrays.binarySearch, etc. for plain arrays.

  • Thinking Collections is the parent interface of List/Set
  • Trying to `new Collections()`
  • Confusing Collections.sort with Stream.sorted (different APIs)

Collections utility class
Java 1.2
Interface default methods
Java 1.8

context

open as a page

What are the Java 9+ List.of, Set.of, and Map.of factory methods, and why were they introduced?

level: juniorimportance: must knowfreq 72%

basics

~10 s

List.of, Set.of and Map.of are static methods, added in Java 9, that build small unmodifiable collections in one expression, like List.of(1, 2, 3). Adding to, removing from or replacing in the result throws UnsupportedOperationException.

open as a page

How do Collections.sort and Collections.binarySearch work, and what are their requirements and complexity?

level: middleimportance: must knowfreq 65%

basics

~20 s

Collections.sort(list) orders a list in place, ascending by the elements' natural order or in the order a Comparator you pass defines. Collections.binarySearch(list, key) finds an element quickly, but only works if the list is already sorted.

open as a page

How do List.of / Set.of / Map.of differ from Collections.unmodifiableList / unmodifiableSet / unmodifiableMap?

level: middleimportance: must knowfreq 74%

basics

~20 s

List.of, Set.of and Map.of build a self-contained unmodifiable collection with no backing collection to change. The Collections.unmodifiable* methods only wrap an existing collection in a read-only view, so if the original changes, the view changes too.

open as a page

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

level: juniorimportance: should knowfreq 45%

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.

open as a page

What are the null-element and duplicate-element rules of List.of / Set.of / Map.of, and how do they differ from ArrayList/HashSet/HashMap?

level: middleimportance: should knowfreq 58%

basics

~20 s

List.of, Set.of and Map.of refuse nulls with NullPointerException; Set.of and Map.of also throw IllegalArgumentException on duplicate elements or keys. ArrayList stores nulls and duplicates; HashSet allows one null and ignores duplicates; HashMap allows one null key and overwrites.

open as a page

What do the Collections.synchronizedXxx wrappers provide, what are their limits, and when would you prefer the java.util.concurrent collections?

level: seniorimportance: should knowfreq 50%

basics

~10 s

Collections.synchronizedList/Map wrap a collection so each single call runs under one lock. Iterating still needs manual locking, and one shared lock serializes every guarded call, so concurrent collections like ConcurrentHashMap usually scale better.

open as a page

What does Collections.unmodifiableList return, and how is it different from a collection built by List.of?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Collections.unmodifiableList returns a read-only view of an existing list: you cannot change it through the view, but the original can still change and those changes show up. List.of builds its own unmodifiable list, with no backing list to reach.

open as a page

Why do List.of and Set.of have many fixed-arity overloads plus a varargs form, and how do you build a larger immutable Map?

level: seniorimportance: should knowfreq 42%

basics

~20 s

List.of and Set.of have overloads for 0 to 10 elements to skip the array a varargs call allocates, with varargs for bigger collections. Map.of stops at 10 pairs, so larger maps use Map.entry plus Map.ofEntries.

open as a page

Are collections from List.of / Map.of deeply immutable, and what does that mean for stored mutable objects and thread safety?

level: seniorimportance: should knowfreq 48%

basics

~20 s

List.of and Map.of collections are only shallowly immutable: you cannot add, remove, or replace elements, but a mutable element (an ArrayList, a bean) can still change internally. The collection's structure is fixed; mutable elements bring their own thread-safety hazards.

open as a page