gpt4 book ai didi

java - 如何获得不可修改的 LinkedHashSet

转载 作者:行者123 更新时间:2023-11-29 10:00:55 25 4
gpt4 key购买 nike

Collections.unmodifiableCollection()Collections.unmodifiableSet() 但没有 Collections.unmodifiableLinkedHashSet()

保留顺序的正确选择是什么?

最佳答案

Collections.unmodifiableSet(Set) 返回的 Set只是您作为参数提供的 Set 的包装器。

Returns an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException.

换句话说,所有read Activity 的行为方式与您直接访问目标Set 的方式完全相同。所有写入 Activity 均失败。

所以只需将它与您的 LinkedHashSet 一起使用即可。

LinkedHashSet<?> someSet = ...
Set<?> wrapper = Collections.unmodifiableSet(someSet);

关于java - 如何获得不可修改的 LinkedHashSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29244666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com