我已经列出了其中的两对。我想先根据键对它们进行排序,如果键相同,则根据值进行排序。
我尝试了以下代码,但是抛出了一个异常不兼容类型:无法推断类型变量T。
List<Pair<Integer,Integer>> list = new ArrayList<>();
list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue);错误:
不兼容类型:无法推断类型变量T (参数错配;类对中无效的方法引用方法getKey不能应用于给定类型所需:没有找到参数:对象原因:实际和形式参数列表长度不同) 其中T,K,V是类型变量:t扩展在方法comparingInt(ToIntFunction)中声明的对象(ToIntFunction)K扩展在类对中声明的对象
发布于 2019-08-23 11:02:21
您需要帮助编译器推断出比较器的适当类型:
list.sort(Comparator.<Pair<Integer, Integer>>comparingInt(Pair::getKey).thenComparingInt(Pair::getValue));https://stackoverflow.com/questions/57624889
复制相似问题