我已经列出了成对的。我想先根据键对它们进行排序,如果键相同,则根据值进行排序。
我尝试了以下代码,但引发了不兼容类型的异常:无法推断类型变量T
List<Pair<Integer,Integer>> list = new ArrayList<>();
list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue);
Run Code Online (Sandbox Code Playgroud)
错误:
不兼容的类型:无法推断类型变量T (参数不匹配;类对中的无效方法引用方法getKey无法应用于所需的给定类型:未找到参数:对象原因:实际和正式参数列表的长度不同)
其中T,K,V是类型变量:T扩展在方法compareInt(ToIntFunction)中声明的对象K扩展在类Pair中声明的对象V扩展在类Pair中声明的对象
您需要帮助编译器为比较器推断适当的类型:
list.sort(Comparator.<Pair<Integer, Integer>>comparingInt(Pair::getKey).thenComparingInt(Pair::getValue));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
93 次 |
最近记录: |