pro*_*der 2 java performance list java-8
哪种方式最好,看速度和舒适度?
names.sort( (a,b) -> a.getName().length() - b.getName().length() );
Collections.sort(names, Comparator.comparing( s -> Celebrity.getName().length() ))
BiFunction<Celebrity,Celebrity,Integer> bifunc = (a,b) -> Integer.compare(a.getName().length(), b.getName().length());
Collections.sort( names, bifunc::apply );
Run Code Online (Sandbox Code Playgroud)
Gon*_*ndy 10
一样的.看Collections.sort方法:
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
Run Code Online (Sandbox Code Playgroud)
所有3种方法都按相同的算法排序.
您应该尽可能多地编写代码.除非确实需要,否则不要进行过早的微量优化.
我会用这一行:
names.sort(Comparator.comparingInt(celebrity -> celebrity.getName().length()));
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |