Regarding immutable List (created by Arrays.asList())

蹲街弑〆低调 提交于 2019-11-29 01:26:57

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).

hi.nitish

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!