java 关于不可变列表(由 Arrays.asList() 创建)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25447502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regarding immutable List (created by Arrays.asList())
提问by Venkat
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.
当我们使用从数组创建列表时java.util.Arrays.asList()
,该列表是不可变的。我只是想知道为什么当List
(or Set
or Map
)的基本目的是具有动态大小并且能够随意添加、删除元素时,我们为什么要创建一个不可变列表。当我们需要一个固定大小的数据结构,我们去阵列,当我们需要一个动态的数据结构,我们去List
或Set
或Map
等。那么,什么是具有不可变列表的目的是什么?我在完成任务时遇到了这个问题。
回答by Marco13
When we create a list from an array using java.util.Arrays.asList() , the list is mutable.
当我们使用 java.util.Arrays.asList() 从数组创建列表时,该列表是可变的。
Yes and no: The list may be modified, by calling
是和否:可以通过调用修改列表
list.set(index, element);
But the list may notbe structurallymodified. 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
当我们需要一个固定大小的可变集合时,我们选择 Array
And that's the key point here: An array is nota Collection. The Arrays.asList
method mainly serves as a "bridge" between the "arrays world" and the "collections world".
这就是关键点这里:数组是不是一个集合。该Arrays.asList
方法主要充当“数组世界”和“集合世界”之间的“桥梁”。
The Arrays.asList
method allows you, for example, the pass data to a method that expects a Collection
:
Arrays.asList
例如,该方法允许您将数据传递给需要 的方法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
此应用案例包括从数组创建其他集合。例如,如果您有一个数组并想创建一个Set
包含该数组元素的数组,您可以
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:
但是使用该Arrays.asList
方法,可以更方便地做到这一点:
Set<String> set = new HashSet<String>(Arrays.asList(array));
The Arrays.asList
method is so to say the counterpart of the Collection#toArraymethod, which works in the opposite direction (although this method usually involves creating and filling a newarray, whereas the Arrays.asList
method just "wraps" an array and lets it "look like" a List
).
该Arrays.asList
方法可以说是Collection#toArray方法的对应方法,它以相反的方向工作(尽管此方法通常涉及创建和填充新数组,而该Arrays.asList
方法只是“包装”一个数组并让它“看起来像”一List
)。
回答by krmanish007
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.
java.util.Arrays.asList()
调用return new ArrayList<>(a);
但是这个 ArrayList 是一个私有的 Arrays 类,它扩展AbstractList
并覆盖了一些实现。因此,期望 的行为是不公平的java.util.ArrayList
。如果你仔细观察,java.util.AbstractList
你会发现你可以调用 add(E e) 但没有很多其他方法。因此,根据当前的实现,您可以在列表底部添加一个元素,但不能更改列表的现有结构。
回答by 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
.
这是因为add()
在AbstractList类中,由 Arrays java 类(内部使用)下的自定义 ArrayList(内部静态类)扩展。请注意这个 add() 方法与 java.util.ArrayList 中定义的不同,而是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 arrayusing System.arraycopy()
which eventually calls a native method.
数组具有固定大小的特性,由 jvm 原生提供。即使 Arrays.copyOf() 使用增加的大小,例如Arrays.copyOf(arr, 10); //10 is the length
原始数组是arr= int[]{1,2} // size is two
。它始终创建一个新数组,使用System.arraycopy()
它最终调用本机方法。
[static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)]
[静态无效数组复制(对象src,int srcPos,对象dest,int destPos,int长度)]
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/5620851and https://stackoverflow.com/a/42138471/5620851
请注意,上面的列表只有大小限制,如果您真的想让可变列表不可变,请尝试使用Collections.unmodifiableList(mutableList);
Immutablility 不是 jvm 定义的概念,而是开发人员的想法。请参考https://stackoverflow.com/a/42071121/5620851和https://stackoverflow.com/a/42138471/5620851