简体   繁体   中英

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? 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 . That is because list.sort is a so-called default interface method , which is a Java feature that did not exist back then. Hence why Collections.sort exists.

Now that default methods for interfaces is available, list.sort also exists, because java is OOP and 'thingYouWantToDoSomethingWith.theThingYouWantToDo' is better style.

You should use list.sort(Comparator.naturalOrder()) for that reason. Purely style points - there is no difference in effect or performance whatsoever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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