use of (a,b) -> a-b lambda function java

Solution 1:

The goal of the lambda (a,b) -> a-b is to produce a number that satisfies the Comparator contract:

  • if a less than b - return a negative result
  • if a equals b - return zero
  • if a greater than b - return a positive result.

The value returned by the Comparator is used by sort to compare pairs of elements (numbers) in the collection to decide which is bigger.

The problem is that a - b doesn't work properly for Java integer types1. In some cases, a - b will overflow and you will get a result with the wrong sign. For example, if a is Integer.MIN_VALUE and b is a positive integer, then a - b will be positive when it should be negative.

(This is a fairly common mistake, that even relatively experienced programmers make ... though hopefully, only once.)

By contrast, the Integer.compare(int, int) method gives the correct answer ... for all possible arguments. So better (i.e. more correct) solutions would be:

Collections.sort(list, (a, b) -> Integer.compare(a[1], b[1]));

PriorityQueue<Integer> MaxHeap = new PriorityQueue<>(Integer::compare);

1 - In fact, for any kind of integer type where overflow doesn't trigger an exception of some kind. And for floating point types, there are different gotchas.