When we create a list from an array using java.util.Arrays.asList()
, the list is immutable. I am just curious to know why do we want to create a immutable list when the basic purpose of List
(or Set
or Map
) is to have dynamic size and to be able to add, remove elements at will. When we need a fixed size data structure we go for Array and when we need a dynamic data structure we go for List
or Set
or Map
etc. So what is the purpose of having a immutable list? I came across this while working on my assignment.

- 325
- 1
- 4
- 8
-
By mutable, you mean immutable? – WilQu Aug 22 '14 at 13:02
-
What do you mean by "in this case"? Generally I find your question hard to understand. You seem to mix up concepts of Arrays and Lists. – Lars Blumberg Aug 22 '14 at 13:03
-
Sorry for mistakes in my question.corrected them now.Please check. – Venkat Aug 22 '14 at 17:30
4 Answers
When we create a list from an array using java.util.Arrays.asList() , the list is mutable.
Yes and no: The list may be modified, by calling
list.set(index, element);
But the list may not be structurally modified. That means that it is not possible to add elements to the list or remove elements from the list. The reason simply is that the list is still backed by the array, and the size of the array may not change.
When we need a fixed size mutable collection we go for Array
And that's the key point here: An array is not a Collection. The Arrays.asList
method mainly serves as a "bridge" between the "arrays world" and the "collections world".
The Arrays.asList
method allows you, for example, the pass data to a method that expects a Collection
:
// A method that expects a collection:
void process(List<String> strings) { ... }
void call()
{
String array[] = new String[] { "A", "B", "C" };
// Pass the array (as a list) to the method:
process(Arrays.asList(array));
}
This application case includes creating other collections from an array. For example, if you have an array and want to create a Set
containing the elements from the array, you could to
String array[] = new String[] { "A", "B", "C" };
Set<String> set = new HashSet<String>();
for (String s : array)
{
set.add(s);
}
But with the Arrays.asList
method, this can be done more conveniently:
Set<String> set = new HashSet<String>(Arrays.asList(array));
The Arrays.asList
method is so to say the counterpart of the Collection#toArray method, which works in the opposite direction (although this method usually involves creating and filling a new array, whereas the Arrays.asList
method just "wraps" an array and lets it "look like" a List
).

- 53,703
- 9
- 80
- 159
-
2I agree that asList method serves as bridge between Arrays world and Collections world.I also agree that there are ways to create a mutable list from an Array.But I don't understand clearly why a list ceated by asList is immutable? What is the main purpose of making it immutable ? – Venkat Aug 23 '14 at 08:46
-
2@Venkat I already said that its elements may be modified, but not the size of the list. And the reason for that is that the `List` interface is implemented so that the actual *data* is still stored in the array. And in Java, the size of an array can not change. If you want to create a mutable list from an array, you can use `new ArrayList
(Arrays.asList(array));`. So they did "not intentionally" make the list structurally immutable. It is only a side-effect of the fact that the list is backed by an array. – Marco13 Aug 23 '14 at 13:07 -
`public E set(int index, E element) { throw new UnsupportedOperationException(); }` – krmanish007 Dec 04 '18 at 10:18
-
Having the API provide an "add" method and disabling that method by means of throwing UnsupportedOperationException seems like a very very bad idea... – Aykhan Hagverdili Apr 19 '22 at 10:09
java.util.Arrays.asList()
calls return new ArrayList<>(a);
but this ArrayList is a private class of Arrays which extends AbstractList
and override some implementation. So, it's unfair to expect a behavior of java.util.ArrayList
. If you look into java.util.AbstractList
you will see that you can call add(E e) but not many other methods. So, as per the current implementation, you can add an element at the bottom of the list but can't change the existing structure of the list.

- 6,749
- 16
- 58
- 100
-
Always thought the `ArrayList` returned is the `java.util.ArrayList`. Never knew it was a completely different class. – Arun Gowda Dec 22 '21 at 11:33
If you call System.out.println(Arrays.asList("a", "b").getClass());
the runtime type is java.util.Arrays$ArrayList
,
but if you call System.out.println(new ArrayList<>(Arrays.asList("a", "b").getClass()));
the runtime type is java.util.ArrayList
.
Maybe this distinction helps.

- 1,743
- 17
- 16
It is because of add()
in class AbstractList, extended by the customised ArrayList (inner static class) under Arrays java class(that is used internally). Please note this add() method is not the same defined in java.util.ArrayList but is that of java.util.Arrays$ArrayList
.
An array has the property of being fixed size, provided natively by jvm. Even if, Arrays.copyOf() is used with increased size such as Arrays.copyOf(arr, 10); //10 is the length
, where the original array is arr= int[]{1,2} // size is two
. It creates always a new array using System.arraycopy()
which eventually calls a native method.
[static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)]
Please note, the above list has size restrictions only, if you really want to make a mutable list immutable, please try using Collections.unmodifiableList(mutableList);
Immutablility is not a jvm defined concept but is a developer thought. please refer https://stackoverflow.com/a/42071121/5620851 and https://stackoverflow.com/a/42138471/5620851