EDIT: This is actually a question on leetcode. So the benchmarking is performed by leetcode.
Somewhere in my program, I have to sort a list of Map.Entry<Character, Integer>
. Originally, I used the traditional way to override the Comparator for the sort. Then, I also tried another approach with lambda expression.
However, the performance with the use of lambda expression is way worse than the original approach (4x ms vs 1x ms). All the codes in the two program are the same. The only difference is this one line.
Original Code:
list.sort(new Comparator<Map.Entry<Character,Integer>>()
{
@Override
public int compare(Map.Entry<Character,Integer> o1, Map.Entry<Character,Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
New Code:
list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));