Sorting a List of Pairs java [duplicate]
问题 This question already has answers here : Very confused by Java 8 Comparator type inference (4 answers) Closed 5 months ago . I've List with Pairs in it. I want to sort them according to the keys first and if keys are same then sort according to the values. I tried following code but throws an exception incompatible types: cannot infer type-variable(s) T List<Pair<Integer,Integer>> list = new ArrayList<>(); list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue); Error