不可变对不可修改集合 [英] Immutable vs Unmodifiable collection

查看:136
本文介绍了不可变对不可修改集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

集锦框架概述


不支持修改操作的集合(例如 add / code>和 clear )被称为不可修改。不可修改的集合

Collections that do not support modification operations (such as add, remove and clear) are referred to as unmodifiable. Collections that are not unmodifiable are modifiable.

另外保证 $ c>对象将被看到被称为 immutable 。不可变的集合可变

Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable. Collections that are not immutable are mutable.

我不能理解这种区别。

不可修改不可变之间的区别是什么?

I cannot understand the distinction.
What is the difference between unmodifiable and immutable here?

推荐答案

不可修改的集合通常是可修改集合的包装函式,其他代码仍可存取。因此,如果您只有对不可修改的集合的引用,则 不能对其进行任何更改,因此您不能依赖内容不变。

An unmodifiable collection is often a wrapper around a modifiable collection which other code may still have access to. So while you can't make any changes to it if you only have a reference to the unmodifiable collection, you can't rely on the contents not changing.

不可变的 集合保证无任何东西可以更改集合。如果它包装可修改的集合,则确保没有其他代码可以访问该可修改的集合。注意,虽然没有代码可以改变集合包含引用的对象,对象本身仍然是可变的 - 创建 StringBuilder 的不可变集合不会冻结那些对象。

An immutable collection guarantees that nothing can change the collection any more. If it wraps a modifiable collection, it makes sure that no other code has access to that modifiable collection. Note that although no code can change which objects the collection contains references to, the objects themselves may still be mutable - creating an immutable collection of StringBuilder doesn't somehow "freeze" those objects.

基本上,区别在于其他代码是否可以更改背后的集合。

Basically, the difference is about whether other code may be able to change the collection behind your back.

这篇关于不可变对不可修改集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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