Why Arrays.asList() Throws UnsupportedOperationException When Modifying the Resulting List

The article explains that Java's Arrays.asList() returns a fixed‑size list backed by an internal array, so calling add, remove, or clear on the returned list triggers UnsupportedOperationException, and shows how to avoid the issue by wrapping it in a mutable ArrayList.

Architecture Digest
Architecture Digest
Architecture Digest
Why Arrays.asList() Throws UnsupportedOperationException When Modifying the Resulting List

Alibaba's Java development guidelines warn that the utility method Arrays.asList() should not be used with collection‑modifying operations because its add, remove and clear methods throw UnsupportedOperationException.

Problem Analysis

We start with a simple test:

public static void main(String[] args) {
    List<String> 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 guideline.

To understand why, we look at the implementation of Arrays.asList():

public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}

Although the method appears to return a normal ArrayList, the actual class instantiated is an internal static class inside java.util.Arrays, not the public java.util.ArrayList we normally use.

private static class ArrayList<E> extends AbstractList<E>
        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> T[] toArray(T[] a) {
        int size = size();
        if (a.length < size)
            return (T[]) Arrays.copyOf(this.a, size, (Class<? extends T[]>) a.getClass());
        System.arraycopy(this.a, 0, a, 0, size);
        if (a.length > size) a[size] = null;
        return a;
    }
    // other methods omitted
}

This internal class does not implement add, remove or clear; those methods are inherited from AbstractList and simply throw UnsupportedOperationException:

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

public E remove(int index) {
    throw new UnsupportedOperationException();
}

Therefore, any attempt to modify the list returned by Arrays.asList() results in the exception.

Summary

Do not misuse Arrays.asList(); it is backed by a fixed‑size array.

If you need a mutable list, avoid calling collection‑modifying methods on the result.

Wrap the result in a real ArrayList, e.g.,

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));

, to obtain a mutable list.

Reference: https://juejin.im/post/5d10e52ee51d454f6f16ec11
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendjavaCollectionsArrays.asListUnsupportedOperationException
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.