Why Arrays.asList Can Trap You: Common Mistakes and How to Avoid Them
This article explains why using Java's Arrays.asList can lead to unexpected behavior, illustrates three typical pitfalls with primitive arrays, mutable lists, and unsupported modifications, and provides detailed analysis, source code insights, and practical alternatives for safely converting arrays to collections.
1. Common Misuses of Arrays.asList
Many developers use Arrays.asList() to convert an array or a few elements into a collection, but the resulting collection may not behave as expected.
1) Mistake One: Passing a primitive array as the argument
The primitive array is treated as a single generic object, so the list contains only one element—the whole array.
Guess the output?
2) Mistake Two: Modifying the original array or the list after calling asList
Since the list directly references the original array, any change to the array is reflected in the list and vice‑versa, which can cause subtle bugs.
Guess the output?
3) Mistake Three: Adding or removing elements from the list
The list returned by asList() does not override add or remove; it delegates to the parent AbstractList, which throws an exception.
Guess the output?
Answer: No, the list is not a regular java.util.ArrayList.
2. In‑Depth Exploration
By inspecting the source of Arrays.asList() and debugging in IDEA, we discover that it returns an instance of java.util.Arrays.ArrayList, an internal static class of Arrays.
The returned class is java.util.Arrays.ArrayList, which differs from the regular java.util.ArrayList in two main ways.
3. Differences
Fixed size: it does not override add or remove , so its size is immutable after creation.
Parameter assignment: it holds a direct reference to the original array, meaning both share the same underlying data.
In contrast, java.util.ArrayList copies the elements into its own internal array.
4. How to Handle Primitive Types
4.1 Using Spring
4.2 Using Java 8 Streams
5. Converting an Array to an ArrayList
5.1 Iterative conversion (not elegant)
Obviously this approach is verbose.
5.2 Using a utility class
The utility still relies on the same iteration internally.
5.3 Using Java 8
Streams can handle primitive arrays and return the desired collection directly.
5.4 Combining two collection classes
Pass the result of Arrays.asList() to the ArrayList constructor.
6. Final Thoughts
Pay attention to these subtle details; they reflect programming literacy and help avoid hidden pitfalls.
Did you grasp this knowledge point?
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
