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.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
java.util.Arrays.asList()
callsreturn new ArrayList<>(a);
but this ArrayList is a private class of Arrays which extendsAbstractList
and override some implementation. So, it's unfair to expect a behavior ofjava.util.ArrayList
. If you look intojava.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.Yes and no: The list may be modified, by calling
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.
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 aCollection
: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 toBut with the
Arrays.asList
method, this can be done more conveniently: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 theArrays.asList
method just "wraps" an array and lets it "look like" aList
).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 ofjava.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 isarr= int[]{1,2} // size is two
. It creates always a new array usingSystem.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