Java 将保持插入顺序且无重复的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16480529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Collection that will maintain insertion order and no duplicates
提问by Vishwanath.M
In Java collection which collection will doesn't allow duplicates and which also preserve insertion order of data?
在 Java 集合中哪个集合不允许重复,哪个还保留数据的插入顺序?
采纳答案by sanbhat
LinkedHashSet
As per the documentation
根据文档
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)
此实现与 HashSet 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,也就是元素被插入到集合中的顺序(插入顺序)
回答by Sam
LinkedHashSet
does both of them
LinkedHashSet
他们俩都做
Set set = new LinkedHashSet();
回答by Arun P Johny
You can check LinkedHashSetfor this purpose.
为此,您可以检查LinkedHashSet。
A Setwill not allow duplicate values. And LinkedHashSetwill preserve insertion order.
一组将不允许重复的值。而LinkedHashSet将保留插入顺序。
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.)
Set 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashSet 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,也就是元素被插入到集合中的顺序(插入顺序)。请注意,如果将元素重新插入到集合中,则插入顺序不会受到影响。(如果 s.add(e) 在 s.contains(e) 将在调用之前立即返回 true 时调用,则元素 e 被重新插入到集合 s 中。)
回答by Duncan Jones
A LinkedHashSet
should fit the bill.
ALinkedHashSet
应该符合要求。
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).
Set 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashSet 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,也就是元素被插入到集合中的顺序(插入顺序)。
回答by Suresh Atta
Use
用
public class LinkedHashSet<E> extends HashSet<E>
Basically Set won't allow duplicates and
基本上 Set 不允许重复和
This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)
这个链表定义了迭代顺序,也就是元素被插入到集合中的顺序(插入顺序)
http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html
http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html
回答by Stefano Sanfilippo
You want an ordered set, which is implemented by LinkedHashSet.
您需要一个有序集,它由LinkedHashSet实现。