对LinkedHashSet进行排序 [英] Sorting a LinkedHashSet

查看:1899
本文介绍了对LinkedHashSet进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以对LinkedHashSet进行排序。我试过声明

I'm wondering if it's possible to sort a LinkedHashSet. I've tried the statement

Collections.sort((List< Comparable>段落);

然而,这只是抛出一个错误,它不能被转换为List。有没有办法做到这一点,或者我应该使用另一个数据结构?

However, that just throws an error that it cannot be casted into a List. Is there a way of doing this, or should I use another Data Structure?

推荐答案

你应该使用 SortedSet ,例如 TreeSet ConcurrentSkipListSet 如果您关心基于比较的排序(例如,排序顺序)。

You should use a SortedSet such as TreeSet or ConcurrentSkipListSet if you care about ordering based on comparison (e.g., sorted order).

A LinkedHashSet 保留基于的订单广告订单

A LinkedHashSet preserves order based on insertion order.

如果您真的想使用 Collections.sort ,您可以转换通过实际构建列表将LHS列入List中(尽管问题不是告诉我们段落的类型所以我假设它是 String

If you really want to use Collections.sort you can convert the LHS into a List by actually constructing a List (though the question doesn't tell us the type of paragraph so I'll assume it is String)

List<String> listParagraph = new ArrayList<String>(paragraph);
Collections.sort(listParagraph)

但这可能不是最佳方法。

but that's probably not the best approach.

这篇关于对LinkedHashSet进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆