Medium👤 0-2 years👤 3-5 years 1 min read

What are the pitfalls of Arrays.asList(), and how do they bite in production?

Asked inTCSInfosysAccentureCapgemini
#arrays.aslist#fixed-size#autoboxing#backed list
Report issue

⚡ 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

Fixed sizeadd/remove → UnsupportedOperation
Backedset() mutates the original array
int[] trapList<int[]> of size 1, not List<Integer>

⌨️Hands-on Keyboard

Learn by Doing

java
int[] a = {1,2,3};
List<int[]> wrong = Arrays.asList(a);      // size 1!
List<Integer> ok = Arrays.stream(a).boxed()
                         .collect(Collectors.toList());
Output
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:

fixed-sizewrite-through to arrayint[] vs Integer[] boxingwrap 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

  • 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

List.ofIntStream.boxedArrayList

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