Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.asList give UnsupportedOperationException [duplicate]

The List returned by Arrays.asList can't be modified by using methods such as add or remove. But if you pass it to the Collections.sort method, it can sort the array without any problem (I expected an exception). This seems like a very inconsistent behaviour. So what are the allowed operations on the List, which returned by asList method?

List<Integer> list = Arrays.asList(5,7, 10 , 8,9);
list.remove(2);//Exception

Collections.sort(list);//Ok, No Exception Sort...
System.out.println(list);

I couldn't find any clue for this in the documentation.

Edit: Yes I can understand why it doesn't support remove or add. But then how can it support sorting?

like image 811
Sameera Kumarasingha Avatar asked May 11 '15 18:05

Sameera Kumarasingha


People also ask

Does arrays asList make a copy?

The method only creates a List wrapper upon the underlying array. Because of which, both the array and the newly created list continue to refer to the exact same elements. That is why, no elements are copied when we use asList method.

What does array asList return?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.

What causes UnsupportedOperationException?

An UnsupportedOperationException is a runtime exception in Java that occurs when a requested operation is not supported. For example, if an unmodifiable List is attempted to be modified by adding or removing elements, an UnsupportedOperationException is thrown.

How is array asList () different?

asList method returns a type of ArrayList that is different from java. util. ArrayList. The main difference is that the returned ArrayList only wraps an existing array — it doesn't implement the add and remove methods.


2 Answers

Arrays.asList returns a fixed size List backed by an array. Therefore remove and add are not supported. set is supported. You can look at this List as if it behaves exactly like an array. An array has a fixed length. You can't add or remove elements, but you can assign values to the indices of the array, which is equivalent to the set method of List. And you can sort an array.

Collections.sort(list) doesn't change the size of the List, so is can sort a fixed size list. All you need in order to sort a List is to swap elements of the List. For this purpose set(index,element) is sufficient.

All this information is found in the Javadoc of Arrays :

/**
 * Returns a fixed-size list backed by the specified array.  (Changes to
 * the returned list "write through" to the array.)  This method acts
 * as bridge between array-based and collection-based APIs, in
 * combination with {@link Collection#toArray}.  The returned list is
 * serializable and implements {@link RandomAccess}.
 *
 * <p>This method also provides a convenient way to create a fixed-size
 * list initialized to contain several elements:
 * <pre>
 *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
 * </pre>
 *
 * @param a the array by which the list will be backed
 * @return a list view of the specified array
 */
 public static <T> List<T> asList(T... a)

And if you look at an implementation of Collections.sort, you see that it actually sorts an array. The only List method it requires that modified the List is set of the List's ListIterator, which calls the List's set(index,element) method.

public static <T extends Comparable<? super T>> void sort(List<T> list) {
  Object[] a = list.toArray();
  Arrays.sort(a);
  ListIterator<T> i = list.listIterator();
  for (int j=0; j<a.length; j++) {
      i.next();
      i.set((T)a[j]);
  }
}
like image 123
Eran Avatar answered Sep 19 '22 22:09

Eran


Arrays.asList gives you a List backed by the array you give it. Arrays are fixed size. The wrapping List supports the same operation that arrays do, so you can reassign the entries, but you can't change its length.

like image 22
khelwood Avatar answered Sep 19 '22 22:09

khelwood