What are the pitfalls of Arrays.asList(), and how do they bite in production?
⚡ Short Answer
Arrays.asList returns a fixed-size list backed by the array: add/remove throw UnsupportedOperationException, and changes write through to the array. With a primitive array it returns a single-element List<int[]>, not a List<Integer>.
☕Coffee Chat Question
Concept Made Simple
“What are the pitfalls of Arrays.asList(), and how do they bite in production?”
🧠Mind Map Answer
Remember It Faster
⌨️Hands-on Keyboard
Learn by Doing
int[] a = {1,2,3};
List<int[]> wrong = Arrays.asList(a); // size 1!
List<Integer> ok = Arrays.stream(a).boxed()
.collect(Collectors.toList());wrong.size() == 1
🔥What If?
Think Beyond the Expected
You pass Arrays.asList(...) to a method that calls add() and it crashes — how do you fix it?
Wrap it in a real resizable list: new ArrayList<>(Arrays.asList(...)). The fixed-size view only supports set(), not structural changes.
😂Real World
The primitive-array gotcha silently produces a size-1 list (List<int[]>), causing 'why is my list empty/size 1?' bugs; and passing the fixed-size view where mutation is expected throws at runtime.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Calling add/remove on the returned list
- ✗Passing a primitive array expecting autoboxing
- ✗Assuming it's a defensive copy (it's a view)
✅Best Practices
- ✓Wrap with new ArrayList<>(...) when you need to mutate
- ✓Use streams + boxed() for primitive arrays
- ✓Prefer List.of for immutable literals
🔁Follow-up Questions
- 1How do you convert a primitive array to a List<Integer> correctly?
- 2Why does set() work but add() throw?
- 3How does List.of differ from Arrays.asList?
🧩Related Technologies
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: List (Java Collections) Interview question: "What are the pitfalls of Arrays.asList(), and how do they bite in production?" 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.
Was this answer helpful?
⭐ Featured Products
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.