Java 订购哈希集示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3380312/
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
ordering a hashset example?
提问by Jony
I need an example on how to use a comparable class on a HashSet
to get an ascending order. Let's say I have a HashSet
like this one:
我需要一个关于如何在 a 上使用可比较的类HashSet
来获得升序的示例。假设我有一个HashSet
这样的:
HashSet<String> hs = new HashSet<String>();
How can I get hs
to be in ascending order?
我怎样才能达到hs
升序?
采纳答案by BalusC
Use a TreeSet
instead. It has a constructor taking a Comparator
. It will automatically sort the Set
.
使用 aTreeSet
代替。它有一个构造函数采用Comparator
. 它会自动对Set
.
If you want to convert a HashSet
to a TreeSet
, then do so:
如果要将 a 转换HashSet
为 a TreeSet
,请执行以下操作:
Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);
// Now it's sorted based on the logic as implemented in YourComparator.
If the items you have itself already implements Comparable
and its default ordering order is already what you want, then you basically don't need to supply a Comparator
. You could then construct the TreeSet
directly based on the HashSet
. E.g.
如果您自己已经实现的项目Comparable
并且其默认订购顺序已经是您想要的,那么您基本上不需要提供Comparator
. 然后,您可以TreeSet
直接基于HashSet
. 例如
Set<String> hashSet = getItSomehow();
Set<String> treeSet = new TreeSet<String>(hashSet);
// Now it's sorted based on the logic as implemented in String#compareTo().
See also:
也可以看看:
回答by trashgod
HashSet
"makes no guarantees as to the iteration order of the set." Use LinkedHashSet
instead.
HashSet
“不保证集合的迭代顺序。” 使用LinkedHashSet
来代替。
Addendum: I would second @BalusC's point about implementing Comparable
and express
a slight preference for LinkedHashSet
, which offers "predictable iteration order ... without incurring the increased cost associated with TreeSet
."
附录:我将第二个@BalusC 关于实现的观点Comparable
并表达对 的轻微偏好LinkedHashSet
,它提供“可预测的迭代顺序......而不会导致与 相关的成本增加TreeSet
。”
Addendum: @Stephen raises an important point, which favors @BalusC's suggestion of TreeMap
. LinkedHashSet
is a more efficient alternative only if the data is (nearly) static and already sorted.
附录:@Stephen 提出了一个重要观点,它支持@BalusC 的TreeMap
. LinkedHashSet
仅当数据(几乎)是静态的并且已经排序时才是更有效的替代方案。
回答by Gian
HashSets do not guarantee iteration order:
HashSets不保证迭代顺序:
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
这个类实现了 Set 接口,由一个哈希表(实际上是一个 HashMap 实例)支持。它不保证集合的迭代顺序;特别是,它不保证订单会随着时间的推移保持不变。此类允许空元素。
You probably need to choose a different datastructureif you want to be able to control the iteration order (or indeed have one at all!)
如果您希望能够控制迭代顺序(或者确实有一个!),您可能需要选择不同的数据结构。