Medium👤 3-5 years 1 min read

List.of vs Collections.unmodifiableList vs Arrays.asList — what's the difference?

Asked inAmazonMicrosoftCognizant
#immutable#unmodifiable#list.of#defensive copy
Report issue

⚡ Short Answer

List.of (Java 9+) is truly immutable and rejects nulls. Collections.unmodifiableList is a read-only VIEW over a backing list — mutating the backing list still changes it. Arrays.asList is a fixed-size, array-backed view (set ok, add/remove throw).

Coffee Chat Question

Concept Made Simple

List.of vs Collections.unmodifiableList vs Arrays.asList — what's the difference?

🧠Mind Map Answer

Remember It Faster

List.of(...)immutable, no nulls, copy-free
unmodifiableList(x)read-only VIEW of x (x can still change)
Arrays.asList(a)fixed-size, backed by array a

⌨️Hands-on Keyboard

Learn by Doing

java
List<String> view = Collections.unmodifiableList(backing);
backing.add("sneaky"); // view now shows it — NOT truly immutable!

List<String> safe = List.copyOf(backing); // snapshot, immutable

🔥What If?

Think Beyond the Expected

You return unmodifiableList(internal) from a getter — is your object safe?

Callers can't mutate via the view, but YOUR class can still mutate `internal`, and the view reflects it. For a defensive snapshot return List.copyOf(internal) instead, which is an independent immutable copy.

😂Real World

Defensive copies at API boundaries prevent callers from mutating internal state. List.of/List.copyOf are the modern idiom; the old unmodifiable* views are a frequent source of 'how did this list change?' bugs.

🎯Interviewer's Expectation

Keywords they're listening for:

immutable vs unmodifiable viewArrays.asList fixed-size + backednull handlingList.copyOf for snapshots

⚠️Common Mistakes

  • Treating unmodifiableList as a true immutable copy
  • Calling add/remove on Arrays.asList result
  • Passing nulls to List.of

Best Practices

  • Use List.of/Set.of/Map.of for constants
  • Use List.copyOf for defensive snapshots
  • Document mutability at API boundaries

🔁Follow-up Questions

  • 1Why does Arrays.asList(arr).add() throw but set() works?
  • 2How do records + List.copyOf give true immutability?
  • 3What does List.of do with a null element?

🧩Related Technologies

List.of/copyOfrecordsGuava ImmutableList

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Performance (Java Collections)
Interview question: "List.of vs Collections.unmodifiableList vs Arrays.asList — what's the difference?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions