关于不可变列表(由 Arrays.asList() 创建) [英] Regarding immutable List (created by Arrays.asList())
问题描述
当我们使用 java.util.Arrays.asList()
从数组创建列表时,列表是不可变的.我只是想知道为什么我们要创建一个不可变列表,而 List
(或 Set
或 Map
)的基本目的是具有动态大小,并且能够随意添加、删除元素.当我们需要固定大小的数据结构时,我们选择 Array,当我们需要动态数据结构时,我们选择 List
或 Set
或 Map
等. 那么拥有一个不可变列表的目的是什么?我在做作业时遇到了这个问题.
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() 从数组创建列表时,列表是可变的.
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
这就是这里的关键点:数组不是集合.Arrays.asList
方法主要充当数组世界"和集合世界"之间的桥梁".
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".
Arrays.asList
方法允许您,例如,将数据传递给需要 Collection
的方法:
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));
}
此应用案例包括从数组创建其他集合.例如,如果您有一个数组并且想要创建一个包含数组元素的 Set
,您可以
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);
}
但是使用 Arrays.asList
方法,这可以更方便地完成:
But with the Arrays.asList
method, this can be done more conveniently:
Set<String> set = new HashSet<String>(Arrays.asList(array));
Arrays.asList
方法可以说是 Collection#toArray 方法,它的工作方向相反(虽然这个方法通常涉及创建和填充一个新数组,而Arrays.asList
方法只是包装"一个数组并让它看起来像"一个 List
).
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
).
这篇关于关于不可变列表(由 Arrays.asList() 创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!