简体   繁体   English

Collections.sort(list) 和 list.sort(Comparator.naturalOrder()) 在性能方面?

[英]Collections.sort(list) and list.sort(Comparator.naturalOrder()) in term of performance?

my question is about the kind of the List that I have to sort it and its order optimisation in both of these sorters in java?我的问题是关于我必须对其进行排序的列表类型及其在 java 中的这两个排序器中的顺序优化? which one and when I have to use?哪一个以及什么时候必须使用?

The two methods end up executing the same code;这两种方法最终执行相同的代码; there is zero difference between the two.两者之间的差异为零。

Collections.sort predates list.sort . Collections.sort早于list.sort That is because list.sort is a so-called default interface method , which is a Java feature that did not exist back then.那是因为list.sort是所谓的default接口方法,是当时不存在的 Java 特性。 Hence why Collections.sort exists.因此,为什么存在Collections.sort

Now that default methods for interfaces is available, list.sort also exists, because java is OOP and 'thingYouWantToDoSomethingWith.theThingYouWantToDo' is better style.现在接口的默认方法可用, list.sort也存在,因为 java 是 OOP 和 'thingYouWantToDoSomethingWith.theThingYouWantToDo' 是更好的风格。

You should use list.sort(Comparator.naturalOrder()) for that reason.出于这个原因,您应该使用list.sort(Comparator.naturalOrder()) Purely style points - there is no difference in effect or performance whatsoever.纯粹的风格点 - 效果或性能没有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM