What are the pitfalls of Arrays.asList(), and how do they bite in production?
Reviewed by Gurusankar M.
β‘ 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.