UnsupportedOperationException when adding or removing element to a List in java

Scenario

Convert an array to a List using asList method of java.util.Arrays class. Try to add an element to the list returned(by calling add method) or remove an element from the list(using remove method) and you will get something like

Exception in thread “main” java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at TestList.main(TestList.java:15)

Code to reproduce the above error is as below

// create an array
Long[] array = {1l,2l};
//convert it to list
List<Long> list = Arrays.asList(array);
// add to list. This throws error
list.add(3L);

Reason

asList method of java.util.Arrays method returns an object of a class named ArrayList which is nested inside the class java.util.Arrays. This class extends java.util.AbstractList and it does not implement add or remove method. Thus when these methods are called on this list object, it calls add or remove method of java.util.AbstractList class which in turn throws java.lang.UnsupportedOperationException. From java docs of add method of java.util.AbstractList class

throws UnsupportedOperationException if the add operation is not supported by this list

Secondly, the list returned by asList method of java.util.Arrays class is a fixed-size list which means that elements cannot be added to or removed from the list. Java docs of asList method states

Returns a fixed-size list backed by the specified array

If you look at the type of object returned by Arrays.asList method, it is Arrays$ArrayList<E>.



Solution

Our ultimate goal is to create an ArrayList which has the contents of the array and which also supports add and remove operations over it. There are a couple of ways to do it as follows:

Method 1 : Iterating over the array

This method is an inefficient method which involves creating a new ArrayList, iterating over the array and in every iteration adding the element of the array to the list. Code is given below.

Long[] array = { 1l, 2l };
// create a new empty list
List<Long> newList = new ArrayList<Long>();
//iterate over array
for (int i = 0; i < array.length; i++) {
	Long element = array[i];
	//add array element to list
	newList.add(element);
}
System.out.println(newList);

Output
Above code produces the following output

[1, 2, 3]

Method 2 : Creating an ArrayList directly with the contents of array

java.util.ArrayList has a constructor which takes another collection as argument. Create a List using Arrays.asList method as you were using earlier and pass the resultant List to create a new ArrayList object. This approach should be preferable over the first approach.

Long[] array={1l,2l};
//convert it to list
List<Long> list = Arrays.asList(array);
//create a new list with the contents of the above list
List<Long> newList = new ArrayList<Long>(list);
// add to list
newList.add(3L);
System.out.println(newList);

Output
Above code produces the following output

[1, 2, 3]

Let’s tweak in

  1. Any data structure which extends java.util.Collection interface can be supplied while creating a new ArrayList. Thus, objects of type java.util.Set such as HashSet, LinkedHashSet, objects of type java.util.List such as LinkedList, Vector can be used.
  2. An ArrayList uses an array behind the scenes.
  3. The type of ArrayList should be the same as that of the List supplied to it in the constructor.
  4. Though this post uses an ArrayList of type java.lang.Long but it is applicable to list of all types such as java.lang.String, java.lang.Integer etc.

Mark Your Impression

Close Menu