分类图<;整数,浮点>;在Java8中,使用ValueComparator的ByValue给出错误
我已经声明了这样一张地图:分类图<;整数,浮点>;在Java8中,使用ValueComparator的ByValue给出错误,java,sorting,dictionary,Java,Sorting,Dictionary,我已经声明了这样一张地图: Map<Integer, Float> errorMap = new HashMap<> (); 问题是,当我编译此代码时,它会给我以下错误: incompatible types: cannot infer type arguments for TreeMap<> reason: inference variable K has incompatible bounds equality constraints
Map<Integer, Float> errorMap = new HashMap<> ();
问题是,当我编译此代码时,它会给我以下错误:
incompatible types: cannot infer type arguments for TreeMap<>
reason: inference variable K has incompatible bounds
equality constraints: Integer
upper bounds: Float,Object
where K is a type-variable:
K extends Object declared in class TreeMap
不兼容类型:无法推断树映射的类型参数
原因:推理变量K的边界不兼容
等式约束:整数
上限:浮点,对象
其中K是一个类型变量:
K扩展类TreeMap中声明的对象
谁能建议一下如何处理这件事。我正在使用Java 8。在地图声明中 TreeMap
键值的类型为Integer,但在compare()方法中获取值时,使用浮点值作为键。因此,将a和b的类型更改为“整数”。它应该可以正常工作。您的
ValueComparator
非常脆弱,容易出现bug。你最好用这样的东西。
class ValueComparator implements Comparator<Float> {
Map<Integer, Float> map;
public ValueComparator(Map<Integer, Float> base) {
this.map = base;
}
public int compare(Float a, Float b) {
if(map.get(a) <= map.get(b))
return -1;
else
return 1;
}
}
incompatible types: cannot infer type arguments for TreeMap<>
reason: inference variable K has incompatible bounds
equality constraints: Integer
upper bounds: Float,Object
where K is a type-variable:
K extends Object declared in class TreeMap