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>.
On this page
Concept Made Simple
Coffee Chat Question
“What are the pitfalls of Arrays.asList(), and how do they bite in production?”
Remember It Faster
Mind Map Answer
- Fixed size
- add/remove → UnsupportedOperation
- Backed
- set() mutates the original array
- int[] trap
- List<int[]> of size 1, not List<Integer>
Learn by Doing
Hands-on Keyboard
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
Think Beyond the Expected
What If?
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:
- fixed-size
- write-through to array
- int[] vs Integer[] boxing
- wrap in new ArrayList to mutate
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
- How do you convert a primitive array to a List<Integer> correctly?
- Why does set() work but add() throw?
- How does List.of differ from Arrays.asList?
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?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair