I have a the following code:
List<Move> moves = getMoves();
moves.sort(Comparator.comparingInt(Move::getScore).reversed());
which was working great with this as the method definition in Move
:
public int getScore() { return myScore; }
however I then overloaded getScore()
:
public int getScore(boolean doubleIt) { return myScore*2; }
and now the comparingInt()
method above complains unless I do:
Comparator.<Move>comparingInt(Move::getScore).reversed()
Can someone explain why the compiler needs the additional <Move>
when there are references to overloaded methods?
error message is:
incompatible types: cannot infer type-variable(s) T
(argument mismatch; invalid method reference
incompatible types: java.lang.Object cannot be converted to boolean)
This is on MacOS Sierra, JDK is:
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
example code: http://ideone.com/RxcXW6