如何从 Set 和 Comparator 中获取 List

发布于 2024-09-27 23:00:33 字数 102 浏览 0 评论 0原文

Set 获取 List 并根据给定的 Comparator 进行排序的“好”(以及为什么?)解决方案是什么?

What is the "good" (and why ?) solution to get a List from a Set and sorted against a given Comparator ?

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

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

发布评论

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

评论(5

朮生 2024-10-04 23:00:33
Set<Object> set = new HashSet<Object>();

// add stuff

List<Object> list = new ArrayList<Object>(set);
Collections.sort(list, new MyComparator());
Set<Object> set = new HashSet<Object>();

// add stuff

List<Object> list = new ArrayList<Object>(set);
Collections.sort(list, new MyComparator());
时光匆匆的小流年 2024-10-04 23:00:33

只需构建它即可。 ArrayList 有一个 构造函数采用另一个集合

Set<Foo> set = new TreeSet<Foo>(new FooComparator<Foo>());
// Fill it.

List<Foo> list = new ArrayList<Foo>(set);
// Here's your list with items in the same order as the original set.

Just construct it. The ArrayList has a constructor taking another Collection.

Set<Foo> set = new TreeSet<Foo>(new FooComparator<Foo>());
// Fill it.

List<Foo> list = new ArrayList<Foo>(set);
// Here's your list with items in the same order as the original set.
辞别 2024-10-04 23:00:33

要么:

Set<X> sortedSet = new TreeSet<X>(comparator); ...
List<X> list = new ArrayList<X>(sortedSet);

要么:

Set<X> unsortedSet = new HashSet<X>(); ...
List<X> list = new ArrayList<X>(unsortedSet);
Collections.sort(list, comparator);

Either:

Set<X> sortedSet = new TreeSet<X>(comparator); ...
List<X> list = new ArrayList<X>(sortedSet);

or:

Set<X> unsortedSet = new HashSet<X>(); ...
List<X> list = new ArrayList<X>(unsortedSet);
Collections.sort(list, comparator);
相守太难 2024-10-04 23:00:33

假设您从未排序的集合或按不同顺序排序的集合开始,以下可能是最有效的假设您需要可修改的列表。

Set<T> unsortedSet = ... 
List<T> list = new ArrayList<T>(unsortedSet); 
Collections.sort(list, comparator);

如果不可修改的列表是可接受的,那么以下方法会更快一些:

Set<T> unsortedSet = ... 
T[] array = new T[unsortedSet.size()];
unsortedSet.toArray(array);
Arrays.sort(array, comparator);
List<T> list = Arrays.asList(array);

在第一个版本中,Collections.sort(...) 将列表内容复制到数组中,对数组进行排序,然后复制已排序的元素返回到列表。第二个版本更快,因为它不需要复制已排序的元素。

但说实话,性能差异可能并不显着。事实上,随着输入集大小变大,性能将主要由进行排序的 O(NlogN) 时间决定。复制步骤的复杂度为O(N),并且随着 N 的增长,其重要性也会降低。

Assuming that you start with an unsorted set or a set sorted on a different order, the following is probably the most efficient assuming that you require a modifiable List.

Set<T> unsortedSet = ... 
List<T> list = new ArrayList<T>(unsortedSet); 
Collections.sort(list, comparator);

If an unmodifiable List is acceptable, then the following is a bit faster:

Set<T> unsortedSet = ... 
T[] array = new T[unsortedSet.size()];
unsortedSet.toArray(array);
Arrays.sort(array, comparator);
List<T> list = Arrays.asList(array);

In the first version, Collections.sort(...) copies the list contents to an array, sorts the array, and copies the sorted elements back to the list. The second version is faster because it doesn't need to copy the sorted elements.

But to be honest the performance difference is probably not significant. Indeed, as the input set sizes get larger, the performance will be dominated by the O(NlogN) time to do the sorting. The copying steps are O(N) and will reduce in importance as N grows.

简单 2024-10-04 23:00:33

这就是当您拥有 Set 时获取 List 的方式:

List list = new ArrayList(set);

不确定您希望使用 Comparator 做什么。如果 Set 已排序,则列表将包含按排序顺序的元素。

This is how you get a List when you have a Set:

List list = new ArrayList(set);

Not sure what you expect to do with the Comparator. If the Set is sorted, the list will contain the elements in sorted order.

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