关于不可变List(由Arrays.asList()创建) [英] Regarding immutable List (created by Arrays.asList())

查看:232
本文介绍了关于不可变List(由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

这是关键点:数组 a 收藏 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 方法允许您将数据传递给期望<$的方法c $ c>集合:

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 方法,它的工作方向相反(虽然这种方法通常涉及创建和填充 new 数组,而 Arrays.asList 方法只是包装数组并让它看起来像a 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).

这篇关于关于不可变List(由Arrays.asList()创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆