订购哈希集示例?

发布于 2024-09-12 07:00:57 字数 190 浏览 7 评论 0 原文

我需要一个关于如何在 HashSet 上使用类似类来获取升序的示例。假设我有一个像这样的 HashSet

HashSet<String> hs = new HashSet<String>();

如何让 hs 按升序排列?

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:

HashSet<String> hs = new HashSet<String>();

How can I get hs to be in ascending order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-09-19 07:00:57

使用 TreeSet 相反。它有一个 带有Comparator的构造函数。它会自动对 Set 进行排序。

如果您想将 HashSet 转换为 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.

如果您拥有的项目本身已经实现了 Comparable 并且它的默认排序顺序已经是你想要的,那么你基本上不需要提供比较器。然后,您可以直接基于 HashSet 构造 TreeSet。例如,

Set<String> hashSet = getItSomehow();
Set<String> treeSet = new TreeSet<String>(hashSet);
// Now it's sorted based on the logic as implemented in String#compareTo().

另请参阅:

Use a TreeSet instead. It has a constructor taking a Comparator. It will automatically sort the Set.

If you want to convert a HashSet to a TreeSet, then do so:

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.

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:

笨笨の傻瓜 2024-09-19 07:00:57

HashSet "不保证集合的迭代顺序。”使用 LinkedHashSet反而。

附录:我赞同@BalusC关于实现 的观点可比较和快递
稍微偏好 LinkedHashSet< /a>,它提供“可预测的迭代顺序......而不会增加与 TreeSet。”

附录:@Stephen 提出了一个重要观点,支持 @BalusC 对 TreeMap 的建议。仅当数据(几乎)静态且已排序时,LinkedHashSet 才是更有效的替代方案。

HashSet "makes no guarantees as to the iteration order of the set." Use LinkedHashSet instead.

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."

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.

柏林苍穹下 2024-09-19 07:00:57

HashSet 不保证迭代顺序

该类实现了Set
接口,由哈希表支持
(实际上是一个HashMap实例)。它
不保证
集合的迭代顺序;在
特别是,它不保证
订单将保持不变
时间。该类允许 null
元素。

您可能需要选择不同的数据结构如果您希望能够控制迭代顺序(或者确实有一个!)

HashSets do not guarantee iteration order:

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.

You probably need to choose a different datastructure if you want to be able to control the iteration order (or indeed have one at all!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文