Problem Description
When attemptign to remove elements from a list created via Arrays.asList(), a java.lang.UnsupportedOperationException is thrown at runtime:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:64)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractCollection.remove(AbstractCollection.java:293)
at com.example.DebugTest.main(DebugTest.java:17)
Root Cause Analysis
The issue stems from how Arrays.asList() is implemented. When you call this method, it returns a instance of a private inner class that extends AbstractList but does not override the mutation methods.
Sample Code That Triggers the Exception
import java.util.Arrays;
import java.util.List;
public class DebugTest {
public static void main(String[] args) {
String departmentCode = "HR部门〔2023〕12号,财务部门〔2023〕12号";
String employeeIds = "123456789,987654321,456123789";
List<String> deptList = Arrays.asList(departmentCode.split(","));
List<String> idList = Arrays.asList(employeeIds.split(","));
// This will throw UnsupportedOperationException
deptList.remove("HR部门〔2023〕12号");
idList.remove("987654321");
System.out.println(deptList);
System.out.println(idList);
}
}
Source Code Analysis
The Arrays.asList() method is defined as follows:
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
This returns an instance of java.util.Arrays.ArrayList, wich is a private static inner class that extends AbstractList<E>:
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable {
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() {
return a.length;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
// Note: remove() is NOT overridden
}
The AbstractList class provides default implementations for mutation methods that simply throw UnsupportedOperationException:
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
}
Since Arrays.ArrayList does not override these methods, any attempt to modify the list results in this exception.
Solutions
To resolve this issue, wrap the result in a fully mutable list implementation:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class DebugTest {
public static void main(String[] args) {
String departmentCode = "HR部门〔2023〕12号,财务部门〔2023〕12号";
String employeeIds = "123456789,987654321,456123789";
// Solution 1: Wrap in ArrayList
List<String> deptList = new ArrayList<>(Arrays.asList(departmentCode.split(",")));
// Solution 2: Wrap in LinkedList
List<String> idList = new LinkedList<>(Arrays.asList(employeeIds.split(",")));
// Solution 3: Wrap in CopyOnWriteArrayList for thread-safe operations
List<String> threadSafeList = new CopyOnWriteArrayList<>(Arrays.asList(employeeIds.split(",")));
// Now removal operations work correctly
deptList.remove("HR部门〔2023〕12号");
idList.remove("987654321");
System.out.println(deptList);
System.out.println(idList);
}
}
Key Takeaways
Arrays.asList()returns a fixed-size list backed by the original array- This returned list does not support add, remove, or clear operations
- Always wrap
Arrays.asList()in a newArrayList,LinkedList, or other mutable list when modification is required - The returned list supports set() and get() operations but not add() or remove()