Collections.sort(list)和list.sort(Comparator)之间的区别
有什么理由让我更喜欢 Collections.sort(list)
方法而不是简单地调用 list.sort()
吗?在内部, Collections.sort
仍然只调用 List
类的 sort
方法.
Is there any reason why I should prefer Collections.sort(list)
method over simply calling the list.sort()
? Internally Collections.sort
merely calls the sort
method of the List
class anyway.
令人惊讶的是,几乎每个人都告诉我使用 Collections.sort
.为什么?
It's just surprising that almost everyone is telling me to use Collections.sort
. Why?
方法 Collections.sort
自Java 1.2以来一直存在.
The method List.sort(comparator)
that you are refering to was introduced in Java 8, whereas the utility method Collections.sort
has been there since Java 1.2.
这样,您会在Internet上找到很多提及该实用程序方法的参考,但这仅仅是因为它在JDK中已经存在了很长时间.
As such, you will find a lot of reference on the Internet mentioning that utility method but that's just because it has been in the JDK for a lot longer.
请注意, Collections.sort
的实现更改是在8u20 中进行的.