Why Arrays.asList and subList Can Surprise You: Hidden Pitfalls
This article examines common pitfalls when using Java's Arrays.asList and ArrayList.subList methods, explaining why add operations throw UnsupportedOperationException, how subList creates a view that links modifications between original and sublists, and offers best‑practice recommendations to avoid runtime errors.
1. Using Arrays.asList – Things to Watch Out For
1.1 Potential Pitfalls
First, look at the usage of Arrays.asList:
List<Integer> statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));The output is shown in the image below.
When trying to add an element to statusList:
statusList.add(3);
System.out.println(statusList.contains(3));It throws java.lang.UnsupportedOperationException. The reason is that Arrays.asList returns a fixed‑size list backed by an internal ArrayList implementation that does not override add.
“When converting an array to a collection with Arrays.asList() , you cannot use modification methods such as add/remove/clear; they will throw UnsupportedOperationException .” – Alibaba Java Development Manual
Therefore, avoid calling modification methods on the list returned by Arrays.asList.
2. Using ArrayList.subList – Things to Watch Out For
2.1 Simple Usage
List<String> bookList = new ArrayList<>();
bookList.add("遥远的救世主");
bookList.add("背叛");
bookList.add("天幕红尘");
bookList.add("人生");
bookList.add("平凡的世界");
List<String> sub = bookList.subList(3, 5);
System.out.println(bookList);
System.out.println(sub);The result shows that subList returns a view of the original list from fromIndex (inclusive) to toIndex (exclusive).
Modifying the original list’s element values affects the sublist.
Structural modifications to the original list cause ConcurrentModificationException when the sublist is accessed.
Modifying sublist element values affects the original list.
Structural modifications to the sublist affect the original list.
“The exception is not thrown when adding an element, but when iterating the sublist after the original list has been structurally modified.” – Alibaba Java Development Manual
Source code of subList simply returns a new SubList view, which shares the same underlying data structure, so changes are reflected across both lists.
2.6 Summary
The subList method returns a view of the original collection. Non‑structural modifications to either list affect the other, while structural modifications to the original list trigger ConcurrentModificationException. Structural changes to the sublist also affect the original list.
3. Additional Tips
When a method returns a List, consider whether callers are allowed to modify it. If not, document this clearly.
For quick list creation, Guava provides utilities such as Lists.newArrayList(...) and Sets.newHashSet(...).
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.
