Reputation: 57
Which of this ways is the best, looking at speed and comfort?
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 );
Upvotes: 2
Views: 177
Reputation: 5295
It's the same. look at Collections.sort
method :
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
All 3 approaches are sorted by the same algorithm.
You should write code as much as readable as possible. Don't do premature micro-optimalizations unless really needed.
I would use this one line:
names.sort(Comparator.comparingInt(celebrity -> celebrity.getName().length()));
Upvotes: 10