A List < Integer > a = Arrays. AsList (1, 2, 3, 4); A List < Integer > b = Arrays. AsList (2 and 4); a.retainAll(b);Copy the code

The above code is to take the intersection of the sets A and B. But the code to run when you will be prompted UnsupportedOperationException anomalies.

Arrays.aslist source

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
    return new ArrayList<>(a);
}
Copy the code
  • One of theArrayListNot a class under the java.util package. It’s the inner class of Arrays.
/** * @serial include */ 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 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; } @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; } @Override public int indexOf(Object o) { E[] a = this.a; if (o == null) { for (int i = 0; i < a.length; i++) if (a[i] == null) return i; } else { for (int i = 0; i < a.length; i++) if (o.equals(a[i])) return i; } return -1; } @Override public boolean contains(Object o) { return indexOf(o) ! = 1; } @Override public Spliterator<E> spliterator() { return Spliterators.spliterator(a, Spliterator.ORDERED); } @Override public void forEach(Consumer<? super E> action) { Objects.requireNonNull(action); for (E e : a) { action.accept(e); } } @Override public void replaceAll(UnaryOperator<E> operator) { Objects.requireNonNull(operator); E[] a = this.a; for (int i = 0; i < a.length; i++) { a[i] = operator.apply(a[i]); } } @Override public void sort(Comparator<? super E> c) { Arrays.sort(a, c); }}Copy the code
  • inheritedAbstractListTo achieve theRandomAccessandSerializable.
  • Which implementsAbstractListPart of the method in.
  • When we executeretainAllMethod, which is calledAbstractCollection.retainAll()Methods.
public boolean retainAll(Collection<? > c) { Objects.requireNonNull(c); boolean modified = false; Iterator<E> it = iterator(); while (it.hasNext()) { if (! c.contains(it.next())) { it.remove(); modified = true; } } return modified; }Copy the code
  • When the remove method is executed, an exception is thrown. The reason is that arrays.arrayList doesn’t implement this method. Call the parent class method.
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
Copy the code
  • conclusion

Ali’s code specification that has been clear about the utility class Arrays. The asList () method converts an array into a collection, cannot use the modify the related methods of collection, it’s the add/remove/clear method will throw an UnsupportedOperationException (). The final reason is that there is no concrete implementation. Instead, you call the parent’s method directly. The method of the parent class also has no implementation, but throws an exception directly.