MediumExperience level: 3-5 years1 min read

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

Reviewed by Gurusankar M.

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).

On this page

Concept Made Simple

Coffee Chat Question

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

Remember It Faster

Mind Map Answer

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

Learn by Doing

Hands-on Keyboard

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

Think Beyond the Expected

What If?

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 view
  • Arrays.asList fixed-size + backed
  • null handling
  • List.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

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

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?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.