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
Is java.util.List mutable?
提问by Vineeth Bhaskaran
We are able to add
/remove
elements to List
using add()
/remove()
methods without creating another list which looks similar to StringBuilder
append
. Because of this I think List
is mutable.
我们可以在add
/ remove
elements 中List
使用add()
/remove()
方法,而无需创建另一个类似于StringBuilder
append
. 因此,我认为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 List
is an interface, the only promise it makes is: "these are the methods you will get".
由于List
是一个接口,它做出的唯一承诺是:“这些是您将获得的方法”。
The List
interface 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 List
interface 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 List
implementation Collections.UnmodifiableList
.
Collections.unmodifiableList()
返回一个List
实现的实例Collections.UnmodifiableList
。
Collections.UnmodifiableList
forwards all calls of immutableList
functions, correctly to the underlying List
. Whereas it implements all mutableList
functions 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.List
is 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 List
instance on add()
or remove()
.
java.util.List
是一个接口。实现可以是可变的或不可变的,具体取决于您使用的。如果您想知道java.util.ArrayList
- 它是可变的,并且不会List
在add()
或上创建另一个实例remove()
。
If you are looking for immutable list - check Guava
implementation of ImmutableListor Collections.unmodifiableListwhich throws java.lang.UnsupportedOperationException
on modifications.
如果您正在寻找不可变列表 - 检查ImmutableList或Collections.unmodifiableList 的Guava
实现,它会引发修改。java.lang.UnsupportedOperationException