Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

The JDK added List.of and Map.of in Java 9 with strict rules: no nulls, no duplicates, unspecified iteration order. What do these choices tell you about API design for the long term, and would you apply similar rules to your company's libraries?

The strict rules of List.of and Map.of show three long-term ideas: reject bad input such as null at the point it enters, because a null found later is far from its cause; leave behaviour such as iteration order unspecified and make it visibly random, so callers cannot depend on an implementation detail; and start strict, because a strict rule can be relaxed later without breaking anyone, while a lenient one can never be tightened. I would apply the same ideas to company libraries: validate inputs, specify less, make unspecified behaviour visibly unreliable, and document the rules clearly, since strictness surprises callers at first.

Background to this question

Most API design advice is about naming and shape. The Java 9 collection factories teach something deeper: how to design an API so it can still evolve in twenty years. The JDK team had two decades of experience with the original collections, where every lenient choice became a promise that could never be broken, and they designed the new factories with that in mind. A senior engineer who can read those choices and apply them to internal libraries prevents the compatibility traps that make shared code hard to change. In this answer, we will look at each of the three choices, the principle behind it, and then how to apply the same thinking to our own libraries without making them unpleasant to use.

1A Quick Recap of the Three Rules

List.of, Set.of and Map.of, added in Java 9, return unmodifiable collections with three strict rules. They reject null elements, keys and values with NullPointerException, even in queries such as contains(null). Set.of and Map.of reject duplicates with IllegalArgumentException instead of quietly keeping one. And the iteration order of Set.of and Map.of is unspecified, and the JDK deliberately randomises it per JVM run by mixing a random salt into the layout.

Each rule surprised developers at first, and each was a conscious decision. Together they express a view of API design that is worth taking apart.

2Rejecting Nulls Early: Fail Where the Cause Is

A null inside a collection is rarely meant. It usually comes from a value that was never set, a failed lookup, or a missing field. If the collection accepts it, nothing goes wrong at that moment. The NullPointerException comes later, when some other code reads the element and calls a method on it, possibly in another thread, another service or another day. The stack trace then points at the reader, not at the code that put the null in, and debugging means working backwards through the data flow.

Rejecting null at creation moves the failure to the cause. The stack trace points at the exact line that built the collection with a bad value. This is the general principle of fail-fast validation at boundaries: the earlier an invariant is checked, the cheaper the bug is to find. Refusing nulls also simplified the implementation, which can use null internally as a marker for empty slots.

3Unspecified, Randomised Order: Protect the Freedom to Change

Any observable behaviour of an API will eventually be depended on by someone, whether or not it is documented. The original HashMap is the classic example: its iteration order was never specified, but it was stable for years, and countless tests and programs relied on it. When the JDK changed the internal hashing in Java 8, many of them broke, and the JDK had to accept the blame for breaking behaviour it had never promised.

Set.of and Map.of avoid that trap by making the unspecified behaviour visibly unreliable. Because the order changes from one run to the next, any code that depends on it fails quickly, during development, while the author still remembers why. The JDK keeps its freedom to change the implementation at any time, because nobody can have come to rely on it. Some other languages do the same; Go, for example, randomises map iteration order for the same reason.

4Strict First: Rules Can Be Relaxed, Never Tightened

The most important lesson is about direction. If an API rejects nulls today and we decide tomorrow to accept them, no existing caller breaks: every call that worked still works. If an API accepts nulls today and we decide to reject them, every caller that passes null breaks, and we cannot even find them all. The same holds for duplicates, for ranges of values, and for formats. Strictness is a one-way door that can be opened later; leniency is a one-way door that can never be closed.

Why the direction of change matters
Change in a later versionEffect on existing callersPossible?
Strict -> lenient (start accepting null)Every working call still worksYes, compatible
Lenient -> strict (start rejecting null)Callers passing null now failNo, breaks users
Unspecified -> specified orderNobody relied on the old orderYes, compatible
Stable but undocumented -> changed orderHidden dependencies breakBreaks in practice
Starting strict keeps every option open; starting lenient closes them.

This is why the factories were designed strict from the start. A Java 9 API that rejected duplicates in Set.of could still be relaxed in Java 30 if a strong case appeared. The old HashSet, which silently accepts duplicates, can never start rejecting them.

5Applying the Same Rules to Company Libraries

I would adopt the same three ideas in our shared libraries, as explicit guidelines. First, validate inputs at the public boundary: Objects.requireNonNull on parameters that must not be null, range checks on numbers, and clear exceptions with messages that name the argument. Second, specify less: document what callers may rely on, and say explicitly what is unspecified, such as the order of results from a set or map. Third, where an unspecified behaviour is likely to be depended on, make it visibly unreliable in development and tests.

Java
public Set<String> tagsFor(String productId) {
Objects.requireNonNull(productId, "productId"); // fail at the cause
Set<String> tags = loadTags(productId);
return Set.copyOf(tags); // unmodifiable, order unspecified
}
// In the library's test helper or debug mode: shuffle unordered results,
// so callers' tests fail early if they depend on order
List<String> shuffled = new ArrayList<>(tags);
Collections.shuffle(shuffled);

Notice that Set.copyOf already gives us the randomised order of the JDK's immutable sets, so returning it applies the third rule for free. For a library that returns lists whose order is not meaningful, a test mode that shuffles them achieves the same effect.

6Documenting Strictness So It Helps Rather Than Annoys

Strict APIs surprise people at first. When Java 9 arrived, many developers were frustrated that List.of rejected contains(null) or that Set.of order changed. Most of that frustration came from rules that were not obvious from the method signature. So the last part of the guideline is documentation: every public method states what it rejects, what is unspecified, and what the caller can rely on, ideally with one example. Exception messages should explain the rule, such as tags must not contain null, not just throw.

With clear documentation, strictness quickly turns from an annoyance into a help, because bugs show up immediately and close to their cause. I would also review new public APIs with one question in mind: if we wanted to change this behaviour in three years, could we? If the answer is no, the API is probably promising too much.

7Follow-up questions

Is strictness ever the wrong choice?

Yes, at the edges where we receive data we do not control, such as parsing external JSON or reading old files. There, being tolerant of harmless variations is often more useful, and the strictness belongs one step inside, when we convert the data into our own types.

How do you introduce stricter validation into an existing lenient library?

Gradually, because tightening breaks callers. First log a warning when the bad input appears, measure how often it happens and who sends it, fix those callers, and only then turn the warning into an exception in a new major version.

Does randomising order in tests really help?

Yes. It turns a hidden dependency that might break years later into a test failure today, while the author is still working on the code. The cost is a small helper and occasionally a test that must be rewritten to compare without order.

8Conclusion

In this answer, we saw that List.of and Map.of reject nulls to fail at the cause, reject duplicates because they signal mistakes, and randomise unspecified order so nobody can depend on it. Behind them is the principle that strict rules can be relaxed later but lenient ones can never be tightened. I would apply the same ideas to company libraries: validate inputs at public boundaries, specify only what callers may rely on, make unspecified behaviour visibly unreliable, and document the rules clearly.

What to say in the interviewAbout 90 seconds aloud

Open with

I read those choices as lessons from the old collections. First, rejecting nulls: a null inside a collection is almost always a bug, and if you accept it, the NullPointerException appears much later, in the code that reads it, far from the cause.

Then, if they want more

Failing at creation puts the stack trace on the line that made the mistake. Second, unspecified and randomised order: anything observable gets depended on. HashMap's order was never promised, but people relied on it for years, and the Java 8 changes broke them.

By randomising Set.of and Map.of per JVM run, the JDK makes such dependencies fail immediately, and keeps its freedom to change the implementation. Third, and most important, strict rules can be relaxed later without breaking anyone, but lenient rules can never be tightened, because some caller is always relying on the leniency. So start strict.

Yes, I would apply the same rules to our libraries: validate inputs at public boundaries with clear messages, document what callers may rely on and explicitly what is unspecified, and where an unspecified behaviour is tempting to depend on, make it visibly unreliable, for example by returning Set.copyOf or shuffling in a test mode.

I would pair this with good documentation, because strictness surprises people at first. And at the edges where we parse external data, I would be a little more tolerant and apply strictness one step inside.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, Java 30, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "The JDK added List.of and Map.of in Java 9 with strict rules: no nulls, no duplicates...": https://scrivio-interview-library.netlify.app/java/collections-overview/43fce53951/