Collections.sort(...)如何工作? [英] How does Collections.sort(...) work?

查看:106
本文介绍了Collections.sort(...)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要清楚,我试图找出Collections.sort(list,new MyComp())方法如何调用比较方法的顺序。

To be clear, I am trying to find out how Collections.sort(list, new MyComp()) method calls the compare method in which sequence.

I有一个LinkedList与员工和他们的个人号码(k):
数字是:
{1,2,3,4,5,6}
比较(对象o1,对象o2 MyComparator中的方法返回一些数字(与此问题无关)。
sort()如何调用方法比较?
是否用1,2参数调用它,2,3然后是3,4然后是4,5然后是5,6?我调试它但是有一些奇怪的序列它会跳回来并且还会比较1,3。

I have a LinkedList with Employees and their personal number (k): The numbers are: {1,2,3,4,5,6} the compare(Object o1, Object o2) method in MyComparator gives back some number (which is not relevant for this concern). How does sort() call the method compare? Does it call it with the parameters 1,2 then, 2,3 then 3,4 then 4,5 then 5,6? I debug it but there's some strange sequence where it jumps back and also compares 1,3.

它到底有什么比较?任何模式?

What exactly does it compare? Any pattern?

推荐答案

具体的比较取决于内部的算法, Collections.sort 方法用于对元素进行排序。根据Javadoc for Collections.sort

The specific comparisons made depend on what algorithm, internally, the Collections.sort method is using to sort the elements. According to the Javadoc for Collections.sort:


多态算法的文档本课程中包含的内容通常包括实施的简要说明。这些描述应被视为实施说明,而不是规范的一部分。只要遵守规范本身,实现者就可以随意替换其他算法。 (例如,sort使用的算法不一定是mergesort,但它必须是稳定的。)

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

In换句话说,Java实现可以自由使用他们喜欢的任何排序算法,前提是它以相同的相对顺序保持相同的元素。这意味着如果不了解您的特定Java实现,就无法知道将要进行哪些比较。 (如果我没记错的话,Oracle版本的Java实际上将其从Java 7的 Collections.sort 的实现切换到Java 8,尽管我可能会弄错。)

In other words, Java implementations are free to use whatever sorting algorithm they'd like, provided that it keeps equal elements in the same relative order. This means that without more knowledge of your specific Java implementation, there's no way to know what comparisons are going to be made. (If I remember correctly, the Oracle version of Java actually switched its implementation of Collections.sort from Java 7 to Java 8, though I may be mistaken.)

那就是说,这不是一件坏事。编写比较器背后的想法是告诉排序方法做任何你需要做的事情来排序,如果你需要进行比较,这就是做这件事的方法。这是一个很好的抽象 - 你说如何对事物进行排名,然后魔术黑盒子排序然后使用它来使事情有序。

That said, that's not a bad thing. The idea behind writing a comparator is to tell the sorting method "do whatever you need to do to sort things, and if you ever need to make a comparison, here's the way to do it." It's a nice abstraction - you say how to rank things, and the Magic Black Box of Sorting then goes and uses it to get things into order.

这篇关于Collections.sort(...)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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