Java中有序集的任何实现?

2022-01-17 00:00:00 set collections java

如果有人熟悉 Objective-C,那么有一个名为 NSOrderedSet 充当 Set,其项目可以作为 Array 的项目访问.

If anybody is familiar with Objective-C there is a collection called NSOrderedSet that acts as Set and its items can be accessed as an Array's ones.

Java 中有这样的东西吗?

Is there anything like this in Java?

我听说有一个集合叫做 LinkedHashMap,但我还没有找到类似的集合.

I've heard there is a collection called LinkedHashMap, but I haven't found anything like it for a set.

推荐答案

看看LinkedHashSet 类

来自 Java 文档:

From Java doc:

Set 接口的哈希表和链表实现,具有可预测的迭代顺序.此实现与 HashSet 的不同之处在于它维护一个双向链表,该列表贯穿其所有条目.这个链表定义了迭代顺序,元素被插入到集合中的顺序(insertion-order).请注意,如果将元素重新插入集合中,插入顺序不会受到影响.(如果在调用 s.contains(e) 将在调用之前立即返回 true 时调用 s.add(e),则元素 e 将重新插入到集合 s 中.).

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.).

相关文章