Let\'s say I have a Pair class
public class Pair {
public P p;
public Q q;
public Pair(P p, Q q) {
this.p = p;
this
It should be:
pairList.sort(Comparator.<Pair, Integer>comparing(Pair::firstValue)
.thenComparing(Pair::secondValue));
First type parameter refers to the type being passed to Comparator. Second type parameter refers to the type that comparator should effectively compare with.
The error seems to be related to Pair
's generic parameters. One workaround it to use an explicit type, as you've attempted:
pairList.sort(Comparator.<Pair>comparingInt(Pair::firstValue).thenComparingInt(Pair::secondValue));
// ^^^^^^
Note the comparingInt()
which reduces the number of parameters you need to specify, and improves performance by avoiding boxing.
Another solution is to parameterize the type reference:
pairList.sort(Comparator.comparingInt(Pair<?,?>::firstValue).thenComparingInt(Pair::secondValue));
// ^^^^^