- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有 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/
我有很多不同类型的对象的LinkedHashSets要从一个java类传递到另一个类,我是否将它们打包到一个更大的对象中(如果可能的话,另一个链接哈希集)或者我只是将它们传递到正常的对象中方式作为参数
有没有一种方法可以对 LinkedHashSet 中的链接进行排序?我知道它保留了添加元素的顺序,但是有没有一种方法可以像链表一样重新排序这些链接,并使其仍然表现出 HashMap 行为? 最佳答案
现有的功能除了保留插入顺序之外,还使用 LinkedHashSet 来存储一组唯一元素。 有一个新要求,要求检索 LinkedHashSet 中已存在的特定元素。即,当尝试添加元素时,该方法应检查
我找到了 3-4 个关于该主题的主题,但它们没有任何帮助,因为我已经尝试了那里的人建议主题开启者的任何内容。它已经在我的代码中了。 // Set the images of users that ow
这个问题已经有答案了: Is there a Collector that collects to an order-preserving Set? (1 个回答) 已关闭 5 年前。 我想以自然顺序
我有一个声明如下的集合 Set orderSet = new LinkedHashSet (); 存储的字符串值是日期和时间约定的组合 orderSet.add(bean.getDate()+","
我一直在将一大块 Java 代码移植到 C++,并且在我离开时不得不实现 LinkedHashSet 之类的东西。我已经使用 Boost 的多索引容器对 LinkedHashSet/Map 进行了合理
我目前正在构建一个 Android 应用程序,在其中显示名人的名言。我有一个主屏幕和另外两个屏幕,其中我显示所有引言,另一个屏幕显示我最喜欢的引言。 因此,当我在 AllQuotesActivity
有 Collections.unmodifiableCollection() 和 Collections.unmodifiableSet() 但没有 Collections.unmodifiableL
我有以下代码: private static class Node { public LinkedHashSet s = new LinkedHashSet(); public N
我正在解决一个问题,我需要存储具有无重复和维护顺序要求的元素。我选择使用 LinkedHashSet 因为它满足了我的两个要求。 假设我有这段代码: LinkedHashSet hs = new L
我正在尝试创建一种搜索算法,将坐标对存储在名为 HashSquareSpec 的包装类中。为了避免重复并保持插入顺序,我将每个 HashSquareSpec 插入到 LinkedHashSet 中。即
LinkedHashSet lHs = new LinkedHashSet(); lHs.add("Beta"); 在编译上面的代码时(tutorialspoint 使用了类似的方法),我得到了错误:
我很好奇是否有人知道为什么我的 LinkHashSet 中的第一个元素似乎允许重复?我正在通过 CodeEval 进行代码挑战,当我输入值 2、2、3、3、3、4、4、4、5、5、6 时,程序的输出显
我有两组 LinkedHashSet 对象,在这个对象中我有其他对象有更多 LinkedHashSet。 我的问题是: equals 方法(默认)是否检查所有内部 HashSets 是否相同?还是我必
构造函数LinkedHashSet(Collection c)假设参数是有序集合,保证其参数的保留顺序?我们如何确定这一点? Javadoc 文档没有说明顺序: Constructs a new li
我有一个简单的问题要问你,我的 Product 类有这样的字段: private Integer id; private String category; private String symbol;
我想存储一个数字列表 1,2,3,4 -(让我们从 List 开始) 我想确保数字是唯一的(好的,很好,Set) 我想保证订单(好的... LinkedHashSet) 我想从列表中获取最后一个元素.
如何遍历 LinkedHashSet 从最后一项到第一项? 最佳答案 如果您想继续使用集合,可以使用以下方法: LinkedHashSet set = ... LinkedList list = ne
我有一个 XML 文件,我需要在其中查找并计算年份标签的出现次数。例如: Found year 2020 10 times. Found year 2017 1 times. Found year 2
我是一名优秀的程序员,十分优秀!