import java.util.*; class X, B extends Comparable> { class Pair implements Comparable { A a; B b; public A getA() { return a; } public B getB() { return b; } @Override public int compareTo(Pair other) { Comparator comparator = Comparator.comparing(Pair::getA).thenComparing(Pair::getB); return comparator.compare(this, other); } } }