Collections.unmodificSet() 在 Java 中做什么?
我可以看到 Collections.unmodifyingSet
返回给定集合的不可修改视图,但我不明白为什么我们不能仅使用 final
修饰符来完成此操作。
根据我的理解,final
声明了一个常量:无法修改的东西。因此,如果一个集合被声明为常量,那么它就不能被修改:不能从集合中删除任何内容,也不能添加任何内容。
为什么我们需要Collections.unmodifyingSet
?
I can see that Collections.unmodifiableSet
returns an unmodifiable view of the given set but I don't understand why we can't just use the final
modifier to accomplish this.
In my understanding, final
declares a constant: something that cannot be modified. So, if a set is declared as a constant then it cannot be modified: nothing can be removed from the set and nothing can be added.
Why do we need Collections.unmodifiableSet
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
final
声明一个无法修改的对象引用,例如创建一个新的
Foo
并将该引用放置在something
中。此后,无法更改something
以指向Foo
的不同实例。这不会阻止修改对象的内部状态。我仍然可以调用 Foo 上相关范围可访问的任何方法。如果这些方法中的一个或多个修改了该对象的内部状态,则
final
不会阻止这种情况。因此,以下内容:
是否不创建一个无法添加或以其他方式更改的
Set
?它只是意味着fixed
将仅引用该实例。相比之下,执行以下操作:
创建一个
Set
实例,如果尝试调用fixed.add()
或,它将抛出
- 对象本身将保护其内部状态并防止其被修改。UnsupportedOperationException
例如,fixed.remove()为了完整起见:
创建一个
Set
实例,该实例不允许更改其内部状态,并且也意味着fixed
只会指向该实例放。final
之所以可以用来创建基元常量,是基于其值无法更改的事实。请记住,上面的fixed
只是一个引用 - 一个包含无法更改的地址的变量。好吧,对于原语,例如ANSWER
的值是 42。由于ANSWER
无法更改,因此它的值永远只有 42。这个例子模糊了所有内容这些行将是这样的:
根据上述规则,
QUESTION
包含代表“终极问题”的String
实例的地址,并且该地址无法更改。这里要记住的是,String
本身是不可变的 - 您不能对String
的实例执行任何更改它的操作,也不能执行任何其他操作(例如replace
、substring
等)返回对完全不同的String
实例的引用。final
declares an object reference that can't be modified, e.g.creates a new
Foo
and places the reference insomething
. Thereafter, it's not possible to altersomething
to point to a different instance ofFoo
.This does not prevent modification of the internal state of the object. I can still call whatever methods on
Foo
there are accessible to the relevant scope. If one or more of those methods modifies the internal state of that object, thenfinal
won't prevent that.As such, the following:
does not create a
Set
that can't be added to or otherwise altered; it just means thatfixed
will only ever reference that instance.By contrast, doing:
creates an instance of a
Set
which will throwUnsupportedOperationException
if one attempts to callfixed.add()
orfixed.remove()
, for example - the object itself will protect its internal state and prevent it from being modified.For completeness sake:
creates an instance of a
Set
which won't allow its internal state to be changed, and also means thatfixed
will only ever point to an instance of that set.The reason that
final
can be used to create constants of primitives is based on the fact that the value can't be changed. Remember thatfixed
above was just a reference - a variable containing an address that can't be changed. Well, for primitives, e.g.the value of
ANSWER
is that 42. SinceANSWER
can't be changed, it will only ever have the value 42.An example that blurs all the lines would be this:
Per the rules above,
QUESTION
contains the address of an instance ofString
which represents "The ultimate question", and that address can't be changed. The thing to remember here is thatString
itself is immutable - you can't do anything to an instance ofString
which changes it, and any operations which would otherwise do so (such asreplace
,substring
, etc.) return references to entirely different instances ofString
.final
仅保证对变量所代表的对象的引用不能更改,它对对象的实例及其可变性没有任何作用。final Set s = new Set();
只是保证您不能再次执行s = new Set();
。它不会使集合不可修改,如果是的话,您一开始就无法向其中添加任何内容。因此,为了明确起见,final
仅影响变量 reference,而不影响引用指向的对象。我可以执行以下操作:
但我不能执行此操作。
再次因为
final
我无法修改变量 l 指向的内容。您必须执行以下三件事之一才能使 Collection 容器线程安全。
或
或
使用 java.util.concurrency.* 包中的适当容器之一。
如果我有一个
Person
对象并执行了final Person p = new Person("me");
这意味着我无法将p
重新分配给指向另一个Person
对象。我仍然可以做p.setFirstName("you");
令人困惑的是,它
看起来像 C++ 中的
const
,而实际上它们指向的对象是不可变的/本质上不可修改。具有可以更改对象内部状态的 mutator 方法的容器或对象不是const
,只是对这些对象的引用是final
并且可以'不能重新分配引用另一个对象。final
only guarantees that the reference to the object the variable represents can't be changed it doesn't do anything for the instance of the object and its mutability.final Set s = new Set();
just guarantees you can't dos = new Set();
again. It doesn't make the set unmodifiable, it if did you couldn't add anything to it to begin with. So to make it really clear,final
only affects the variable reference not the object the reference points to.I can do the following:
but I can't do this.
again because of the
final
I can't modify what the variable l points to.you have to do one of the following three things to make a Collection container thread safe.
or
or
use one of the appropriate containers from
java.util.concurrency.* package
.if I had a
Person
object and didfinal Person p = new Person("me");
it means I can't reassignp
to point to anotherPerson
object. I can still dop.setFirstName("you");
What confuses the situation is that
look like
const
in C++, when in fact the objects that they point to are immutable/unmodifiable by nature. Containers or objects with mutator methods that can alter the internal state of the object are notconst
just the reference to those objects arefinal
and can't be reassigned to reference another object.Collections.unmodifyingSet(Set)
将在原始集合上创建包装器。该包装器集无法修改。但原始设置仍然可以修改。示例:
添加一些元素
打印添加的元素
将
actualSet
放入不可修改的集合中,并分配给新引用 (wrapperSet
)。打印包装器集。因此它将具有
actualSet
值,让我们尝试在
wrapperSet
上删除/添加一个元素。中添加一个元素
在
actualSet
PrintactualSet
和wrapperSet
。两组值相同。因此,如果您在实际集上添加/删除任何元素,则更改也将反映在包装集上。用法:
此
Collections.unmodifyingSet(Set)
用于防止修改任何对象的 Set 的 getter 方法。让我们说The
Collections.unmodifiableSet(Set<? extends T>)
will create wrapper on the original set. This wrapper set can not be modified. but still the original set can be modified.Example:
Adding some elements
Printing added elements
Put the
actualSet
into unmodifiable set and assigned to new reference(wrapperSet
).Print the wrapperSet. so it will have
actualSet
Valueslets try to remove/add one element on
wrapperSet
.Add one more element in
actualSet
Print
actualSet
andwrapperSet
. both sets values are same. so If you add/remove any elements on actual set the changes will be reflected on wrapper set as well.Usage:
This
Collections.unmodifiableSet(Set<? extends T>)
is used to prevent modification of Set's getter method of any object. let sayfinal
不是(C++ 风格)const
。与 C++ 不同,Java 没有 const 方法或类似的东西,可以通过 Final 引用调用可以更改对象的方法。Collections.unmodifying* 是一个包装器,它强制(仅在运行时,而不是在编译时)相关集合的只读性。
final
is not (C++-style)const
. Unlike C++, Java does not haveconst
-methods or anything like that, and methods that can change the object can be called via afinal
reference.Collections.unmodifiable*
is a wrapper that enforces (at run time only, not at compile time) the read-only-ness for the collection concerned.总结一下我们能做和不能做的:
准备工作:
通过引用最终
可以:
不能:
通过引用最终,并且不能通过集合修改。
私人最终设置单词= Collections.unmodifyingSet(单词);
can:
不能
cant:通过引用最终确定,
被集合修改,但作为集合的对象是可变的。 但是如果您有 mutual< 的集合/a> 对象,您可以更改该对象的内部状态。
还是不能
但是可以
Summarize that we can do and can't:
Preparation:
Final by reference
can:
can't:
Final by reference and unmodifiable by collection.
private final Set words = Collections.unmodifiableSet(words);
can:
can't:
Final by reference and unmodifiable by collection but mutable as collection's object.
But if you have the collection with mutual objects you can CHANGE the inner state of that object.
Still can't
But can