Why Arrays.asList() Returns an Unmodifiable List in Java
The article explains that Java's Arrays.asList() creates a fixed‑size list backed by the original array, uses an internal ArrayList class without add, remove, or clear methods, and therefore any attempt to modify the list throws UnsupportedOperationException, with work‑arounds shown.
Alibaba's Java development guidelines warn that using Arrays.asList() to convert an array to a collection should not be followed by modification operations such as add , remove , or clear , because they throw UnsupportedOperationException . This article demonstrates why this happens.
First, a simple test program is shown:
public static void main(String[] args) {
List
list = Arrays.asList("a", "b", "c");
// list.clear();
// list.remove("a");
// list.add("g");
}Uncommenting any of the three lines and running the program reproduces the exception described in the guidelines. To understand the cause, the implementation of Arrays.asList() is examined.
public static
List
asList(T... a) {
return new ArrayList<>(a);
}The method returns a new instance of an internal ArrayList class, which is not the standard java.util.ArrayList . The source of this internal class is:
private static class ArrayList
extends AbstractList
implements RandomAccess, java.io.Serializable {
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() { return a.length; }
@Override
public Object[] toArray() { return a.clone(); }
@Override
@SuppressWarnings("unchecked")
public
T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return (T[]) Arrays.copyOf(this.a, size, (Class
) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size) a[size] = null;
return a;
}
// The following mutating methods are overridden to throw the exception
public void add(int index, E element) { throw new UnsupportedOperationException(); }
public E remove(int index) { throw new UnsupportedOperationException(); }
// other methods omitted for brevity
}Because this internal ArrayList extends AbstractList and does not implement mutating methods, any call to add , remove , or clear ultimately invokes the overridden versions that throw UnsupportedOperationException .
Summary:
Do not misuse Arrays.asList() ; it returns a view backed by the original array.
If you need a mutable list, avoid calling modification methods on the list returned by Arrays.asList() .
Wrap the result in a real ArrayList , e.g., List list = new ArrayList<>(Arrays.asList("a", "b", "c")); , to obtain a fully modifiable list.
There are many ways to convert an array to a collection; see the linked StackOverflow discussion for alternatives.
https://stackoverflow.com/questions/157944/create-arraylist-from-array
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java 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.