skip to content

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%

answer

  1. 0–10 fixed-arity overloads + varargs for 11+
  2. A varargs call allocates an array
  3. Plain fields only at one or two elements
  4. Map.of takes k,v pairs up to 10 entries
  5. Map.entry + Map.ofEntries for larger/clearer maps

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.

solid answer

~50 s

List.of and Set.of ship explicit overloads for 0 through 10 elements, plus a final varargs overload for more. JEP 269's reason is performance: a varargs call allocates and initializes an array to hold the arguments, an overhead the fixed-argument calls avoid at the small sizes these methods are mostly used for. In the current JDK one or two elements then sit in plain fields; from three up the factory still builds an array internally, and the varargs overload also copies its argument array. Map.of has no varargs form: since each entry needs a key and a value, its overloads take pairs (k1, v1, k2, v2, ...) up to 10 entries. Beyond that, or when you want clearer code, you use Map.entry(k, v) to make unmodifiable entries and pass them to Map.ofEntries(...), which is varargs over Map.Entry. All results keep the same mutator and null rules regardless of which overload you hit.

code

java · 12 lines
java
// Up to 10 entries: direct overload
Map<String, Integer> small = Map.of("Ann", 30, "Bob", 25);

// More entries (or clearer grouping): entry + ofEntries
Map<String, Integer> big = Map.ofEntries(
    Map.entry("Ann", 30),
    Map.entry("Bob", 25),
    Map.entry("Cy",  40)
    // ... as many as you need
);

// List/Set: 0..10 use exact overloads; 11+ fall to varargs(E...)

go deeper

for a junior

Knows there are overloads and a varargs form, and that Map uses key/value pairs.

for a middle

Can use Map.ofEntries for large maps and knows the 10-element cutoff.

for a senior

Explains the performance rationale (avoiding the varargs array at the call site, with field-based storage only at the smallest sizes) and that guarantees stay uniform across overloads.

for a principal

Recognizes the pattern as a general API-design technique for hot, small-N call sites and weighs it against API surface bloat and maintenance cost.

## Terms first - *Overload*: multiple methods with the same name but different parameter lists. The compiler picks one based on the arguments you pass. - *Arity*: the number of parameters a method takes. "Fixed-arity" means an exact count (e.g., exactly 3); "varargs" means variable count. - *Varargs* (`String... args`): a method form that accepts any number of arguments. Under the hood Java collects them into an *array*, so calling a varargs method with separate arguments allocates that array. - *Allocation*: reserving heap memory for a new object (here, the argument array). Allocations cost time and create garbage the collector must later clean up. ## Why not just one varargs method? You *could* define only `List.of(E... elements)`. But these factories are aimed at small collections; JEP 269 says "The focus is on small collections." Every such call would allocate and initialize an array just to carry the arguments, and that array would then be garbage, because the varargs overload never keeps it: it copies the elements out, checking each for null. ## The chosen design: many small overloads + one varargs fallback The JDK declares: ```java static <E> List<E> of(); static <E> List<E> of(E e1); static <E> List<E> of(E e1, E e2); // one overload per count, up to: static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10); static <E> List<E> of(E... elements); ``` For 0–10 elements the compiler binds to the exact overload, so the call site allocates no array and looks the same either way. With separate arguments, only exceeding 10 falls through to the varargs version, where an array is unavoidable anyway. JEP 269 accepts the API clutter for that. Behind those overloads the picture is less uniform than "no array". This is an implementation detail, read at JDK 27 and the same at JDK 25: | Call | Internal class | Storage | |---|---|---| | `List.of()`, `Set.of()`, `Map.of()` | one shared instance each | nothing per call | | `List.of(a)`, `List.of(a, b)` | `List12` | two fields | | `Set.of(a)`, `Set.of(a, b)` | `Set12` | two fields | | `Map.of(k, v)` | `Map1` | two fields | | `List.of` with 3–10 elements | `ListN` | one array, built in the overload, no copy | | `Set.of` with 3–10, `Map.of` with 2–10 pairs | `SetN`, `MapN` | a probe table filled from a temporary array | | `List.of(E...)` with 11+ | `ListN` | a copy of the caller's array | The varargs overload has a trap of its own: it also accepts an array. `List.of(array)` makes a list of the array's elements, copied, so later writes to `array` do not show. To get a one-element list holding the array, write `List.<String[]>of(array)`. The same pattern applies to `Set.of`. For `Map.of`, each entry is a key+value, so the overloads take pairs, and there is **no varargs form**: ```java Map.of(); // empty Map.of(k1, v1); // 1 entry Map.of(k1, v1, k2, v2); // 2 entries // and so on, up to 10 entries (20 arguments) ``` ## Building a larger or clearer immutable Map: Map.entry + Map.ofEntries Past 10 entries, the flat `k, v, k, v` list becomes unreadable and there is no 11-pair overload. The solution is two methods: ```java Map<String,Integer> ages = Map.ofEntries( Map.entry("Ann", 30), Map.entry("Bob", 25), Map.entry("Cy", 40) ); ``` - `Map.entry(k, v)` returns an *unmodifiable* `Map.Entry` that rejects nulls. It is not serializable; the Javadoc points to `AbstractMap.SimpleEntry` or `AbstractMap.SimpleImmutableEntry` for that. - `Map.ofEntries(Map.Entry...)` is a varargs method that assembles those entries into an unmodifiable map, throwing `IllegalArgumentException` on a duplicate key. JEP 269 names the cost: each pair has to be boxed in an entry first. This form is also nicer for readability even under 10 entries, because each key/value pair is visually grouped. ## Guarantees are uniform No matter which overload you reach — fixed-arity, varargs, or `ofEntries` — the result follows the same rules: mutators throw, nulls are rejected, and iteration order is unspecified for sets/maps. The overload count is purely an internal optimization; it never changes the contract. ## Practical guidance - ≤10 elements/entries: use the direct `of(...)` overload. - >10 map entries, or when you want grouped readability: use `Map.entry` + `Map.ofEntries`. - Data that is computed or large: fill a `HashMap` and call `Map.copyOf`. JEP 269 lists high-performance collections of arbitrary size as a non-goal. - Don't over-think it for correctness — the optimization is automatic; just know why the API looks the way it does.

  • Why not implement List.of with only a single varargs method?
    Because each varargs call allocates and initializes an array just to pass the arguments, and these factories target small collections. JEP 269 accepts the API clutter of fixed-argument overloads to avoid that overhead. In the current JDK one or two elements then sit in plain fields; larger lists hold a single array.
  • How do you build an immutable Map with 15 entries?
    Use Map.ofEntries(Map.entry(k1,v1), Map.entry(k2,v2), ...), since Map.of only has overloads up to 10 entries.

  • Thinking the overloads change immutability or null rules
  • Assuming there is an unlimited Map.of(k,v,...) for any size
  • Believing varargs is free (it allocates an array)
  • Trying Map.of with 11+ pairs instead of Map.ofEntries
  • Claiming every overload from 0 to 10 stores its elements in fields

Fixed-arity List.of and Set.of overloads
Java 9
Map.entry and Map.ofEntries
Java 9

Assumes JDK 27, where the internal classes were read; the same at JDK 25. The overloads date from Java 9, and which class backs which size is an implementation detail that may change.

context