java.util.List 是可变的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40438544/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:12:41  来源:igfitidea点击:

Is java.util.List mutable?

java

提问by Vineeth Bhaskaran

We are able to add/removeelements to Listusing add()/remove()methods without creating another list which looks similar to StringBuilderappend. Because of this I think Listis mutable.

我们可以在add/ removeelements 中List使用add()/remove()方法,而无需创建另一个类似于StringBuilderappend. 因此,我认为List是可变的。

Can anyone confirm my understanding is correct?

谁能确认我的理解是正确的?

If it is wrong please explain the code below:

如果有错误请解释下面的代码:

List<String> strList = new ArrayList<String>();
strList.add("abc");
strList.add("xyz"); 

回答by Birchlabs

List mutability

列表可变性

Since Listis an interface, the only promise it makes is: "these are the methods you will get".

由于List是一个接口,它做出的唯一承诺是:“这些是您将获得的方法”。

The Listinterface describes a mutableList. Notice that it has functions such as add(), set()and remove(). But notice also that these mutators are designated as "optional" operations.

List接口描述了一个可变列表。请注意,它具有add(),set()和等功能remove()。但也要注意,这些修改器被指定为“可选”操作

There exist implementations of the Listinterface which are, in practice, immutable.

存在List接口的实现,它们在实践中是不可变的。

List<Integer> mutable = new ArrayList<>();
mutable.add(1);

List<Integer> immutable = Collections.unmodifiableList(mutable);
// try to modify the list
immutable.add(2);
// Exception in thread "main" java.lang.UnsupportedOperationException

Collections.unmodifiableList()returns an instance of the Listimplementation Collections.UnmodifiableList.

Collections.unmodifiableList()返回一个List实现的实例Collections.UnmodifiableList

Collections.UnmodifiableListforwards all calls of immutableListfunctions, correctly to the underlying List. Whereas it implements all mutableListfunctions by throwing java.lang.UnsupportedOperationException.

Collections.UnmodifiableList将所有不可变List函数的调用正确转发到底层List. 鉴于,实现所有可变List抛出功能java.lang.UnsupportedOperationException

List element mutability

列表元素可变性

If we do:

如果我们这样做:

List<Date> mutable = new ArrayList<>();
dates.add(new Date());
List<Date> immutable = Collections.unmodifiableList(mutable);
Date myCoolDate = immutable.get(0);

Can we mutate myCoolDate? Absolutely.

我们可以变异myCoolDate吗?绝对地。

myCoolDate.setTime(99999); // works! mutates the original Date object in the `List`.

The List— whether immutable or otherwise — stores copies of references to objects. Once we have a reference to the object (Date myCoolDate = immutable.get(0);), we can mutate the object freely.

The List- 无论是不可变的还是其他的 - 存储对 objects 的引用的副本。一旦我们有了对对象 ( Date myCoolDate = immutable.get(0);)的引用,我们就可以自由地改变对象。

The items in an immutable list have no immutability guarantee. They are exactly as mutable as they have always been.

不可变列表中的项目没有不变性保证。它们完全像往常一样易变

回答by dvelopp

Yes. It's mutable. If you want to have an immutable one, you can wrap it using java utility Collections class: java.util.Collections#unmodifiableList

是的。它是可变的。如果你想要一个不可变的,你可以使用 java 实用程序集合类来包装它: java.util.Collections#unmodifiableList

like this:

像这样:

  List<Object> mutableList = new ArrayList<>();
  List<Object> immutableList = Collections.unmodifiableList(mutableList);

回答by Yuriy Yunikov

java.util.Listis an interface. Implementation could be mutable or immutable depending on which you use. If you're wondering about java.util.ArrayList- it is mutable and it is not creating another Listinstance on add()or remove().

java.util.List是一个接口。实现可以是可变的或不可变的,具体取决于您使用的。如果您想知道java.util.ArrayList- 它是可变的,并且不会Listadd()或上创建另一个实例remove()

If you are looking for immutable list - check Guavaimplementation of ImmutableListor Collections.unmodifiableListwhich throws java.lang.UnsupportedOperationExceptionon modifications.

如果您正在寻找不可变列表 - 检查ImmutableListCollections.unmodifiableList 的Guava实现,它会引发修改。java.lang.UnsupportedOperationException