Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Java chose to express unmodifiable collections as run-time behaviour of the same List and Map interfaces rather than as separate immutable types, unlike Guava's ImmutableList or Kotlin's read-only interfaces. How should that choice shape your organisation's conventions?

Because Java expresses unmodifiable collections as run-time behaviour of the ordinary List and Map interfaces, the type List never tells a caller whether add will work, so our contracts must say it. Guava's ImmutableList makes immutability visible in the type but ties every consumer of our API to Guava, and Kotlin callers see Java collections as platform types and may assume they can change them. The convention I would set is that all collections returned from our APIs are unmodifiable, enforced with List.copyOf, documentation and tests, and revisited only if the JDK ever adds richer collection types.

Background to this question

When the JDK designers added List.of and List.copyOf, they chose not to create new ImmutableList types, so that the unmodifiable lists would work with every existing method that accepts a List. That keeps compatibility, but it pushes a question onto every API we write: will this list throw if someone adds to it? Without an organisation-wide answer, each team answers differently, and callers learn by exception. In this answer, we will look at what the JDK chose and why, the options it leaves us, how Kotlin callers see our collections, the convention I would adopt, and how to keep it honest.

1What the JDK Chose and Why

In Java, List is a single interface with optional operations. The Javadoc for add says that it may throw UnsupportedOperationException if the list does not support it. So an ArrayList, an Arrays.asList result, a Collections.unmodifiableList view and a List.of result all have the same static type, and the compiler cannot tell them apart. The difference only appears when the code runs.

Java
List<String> a = new ArrayList<>(); // add works
List<String> b = List.of("x"); // add throws UnsupportedOperationException
List<String> c = Arrays.asList("x"); // add throws, set works
void process(List<String> items) { items.add("audit"); } // compiles for all three

The JDK designers chose this because a separate ImmutableList type would either have to extend List, inheriting add and so still throwing, or not extend it, making it unusable with the thousands of existing APIs that take a List. Either way it would split the ecosystem. Java 21's SequencedCollection interfaces kept the same approach. The price is that immutability is a property of the object, not of the type, and so it has to be stated in contracts.

2The Options This Leaves Us

The first option is to use a library type that makes immutability visible, such as Guava's ImmutableList, in our API signatures. A method returning ImmutableList<Order> tells every caller, and the compiler, that the result never changes, and Guava marks its mutating methods as deprecated so IDEs warn. The cost is a hard dependency: every consumer of our API must have Guava on the class path, at a compatible version, and Guava upgrades become our consumers' problem too. For a library used by many teams, that coupling is significant.

The second option is to keep java.util.List in signatures and make the behaviour predictable by convention. The type stays standard, and the knowledge moves into documentation, naming and tests. For most organisations this is the better fit, and it is what I would recommend for public APIs.

Ways to express immutability in an API
ApproachVisible in the type?Cost
java.util.List, no conventionnocallers guess; failures at run time
java.util.List plus a conventionno, but predictabledocumentation, review and tests
Guava ImmutableList in signaturesyesevery consumer depends on Guava
Kotlin List (read-only interface)for Kotlin code onlyJava callers still see java.util.List

3How Kotlin Callers See Our Collections

Kotlin has separate read-only and mutable interfaces, List and MutableList, but both compile to java.util.List. When Kotlin code calls a Java method that returns java.util.List, the compiler cannot know which one was meant, so it shows a platform type, written (Mutable)List<String>! in error messages. Kotlin then lets the caller treat it as either.

Java
// Kotlin calling our Java API: List<String> roles()
val roles = javaApi.roles() // platform type (Mutable)List<String>!
val m: MutableList<String> = roles // compiles: Kotlin trusts the developer
m.add("admin") // UnsupportedOperationException at run time if roles() returns List.of(...)

So a Kotlin team may assume a Java list is mutable and only discover otherwise in production. JetBrains nullability and mutability annotations can help Kotlin infer better types, but the simplest protection is again a clear, consistent convention that Kotlin teams know about.

4The Convention I Would Set

The convention is short enough to remember: every collection returned from a public method is unmodifiable, unless the method's name and documentation explicitly say otherwise. Implementations return List.copyOf, List.of, Map.copyOf or Stream.toList() results, never internal mutable collections. Methods that accept collections never modify them; if a method needs to fill a collection, it returns a new one instead of taking an output parameter.

This removes guesswork in the common case. A caller who wants to change a returned list knows to copy it first with new ArrayList<>(result), and a caller who keeps it knows it will not change underneath them. The rare method that returns a live view or a mutable collection becomes the exception that stands out in review.

Java
/**
* Returns the active roles.
* @return an unmodifiable list; copy it to make changes
*/
public List<String> roles() { return roles; } // roles was stored as List.copyOf(input)
List<String> mine = new ArrayList<>(api.roles()); // the caller copies when it needs to change
mine.add("admin"); // fine

5Keeping the Convention Honest

Conventions decay unless something checks them. I would add a contract test helper that each library can use: it calls every public collection-returning method and asserts that add throws UnsupportedOperationException. Static analysis can catch fields of mutable collection types being returned directly. API review checks the Javadoc wording. And the convention goes into the onboarding guide for Kotlin teams, with a note to treat platform-typed collections from our libraries as read-only.

Finally, the decision should be revisited if the JDK changes. If a future Java release adds richer collection types or a standard way to mark immutability in the type, we would evaluate adopting it at a major version. Until then, documentation, tests and copyOf give us the predictability the type system does not.

Step throughReviewing a method that returns a collection
  1. Does it return an internal mutable collection?
    Yesreject: store List.copyOf and return thatNocontinue
  2. Is it a live view or meant to be changed?
    Yesrequire name and Javadoc to say soNocontinue
  3. Javadoc states: unmodifiablethe default contract
  4. Contract test asserts add throwsruns in the library's build

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why not just use Guava types everywhere internally?

Inside one service that already depends on Guava, it is a reasonable choice and makes intent clearer. The concern is public library APIs consumed by many teams, where the type leaks into every caller. A mixed approach, Guava inside, java.util.List at the boundary, is common and works well.

Should methods accept ImmutableList parameters to force callers to pass immutable data?

Usually not. It forces every caller to convert, and the method cannot rely on it anyway unless the elements are immutable too. It is simpler for the method to accept List and call List.copyOf if it needs to keep the data; copyOf costs nothing when the input is already unmodifiable.

What is the risk of the convention for callers that used to mutate returned lists?

Changing an existing method from returning a mutable list to an unmodifiable one is a breaking change for callers that added to it. I would apply the convention to new APIs immediately and to existing ones at a major version, with release notes and a search across known consumers first.

7Conclusion

In this answer, we saw that Java made unmodifiable collections a run-time property of the ordinary List and Map interfaces to stay compatible with existing APIs, so the type never tells a caller whether add will work. Guava's types make immutability visible but tie consumers to Guava, and Kotlin sees Java collections as platform types that may be treated as mutable. The convention that every returned collection is unmodifiable, implemented with copyOf and checked by documentation, contract tests and review, removes the guesswork, and we revisit it only if the JDK adds richer types.

What to say in the interviewAbout 90 seconds aloud

Open with

Java decided that an unmodifiable list is still just a java.util.List, and add simply throws at run time. That kept every existing API that takes a List working, but it means the type tells a caller nothing about whether they can change what they got back.

Then, if they want more

So I think we have to put that information into contracts, and make it predictable. One option is Guava's ImmutableList in signatures, which makes immutability visible to the compiler, but in public libraries it forces every consumer to depend on Guava at a compatible version, which I would avoid at the boundary.

Kotlin adds another wrinkle: Kotlin's read-only and mutable lists both map to java.util.List, so a Java return value arrives as a platform type and Kotlin will happily let a caller treat it as a MutableList and fail at run time.

The convention I would set is that every collection returned from a public method is unmodifiable, implemented with List.copyOf or List.of, and documented as such, and anything that returns a live view or a mutable collection must say so in its name and Javadoc.

I would back it with a small contract test that asserts add throws, static analysis against returning internal fields, and a note for Kotlin teams. And if the JDK ever adds richer collection types, we would revisit it at a major version.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
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 "Java chose to express unmodifiable collections as run-time behaviour of the same List...": https://scrivio-interview-library.netlify.app/java/immutability/86f1898efa/