[英]java unchecked cast
I have a comparator class in Java to compare Map entries: 我在Java中有一个比较器类来比较Map条目:
public class ScoreComp implements Comparator<Object> {
public int compare(Object o1, Object o2) {
Entry<Integer, Double> m1 = null;
Entry<Integer, Double> m2 = null;
try {
m1 = (Map.Entry<Integer, Double>)o1;
m2 = (Map.Entry<Integer, Double>)o2;
} catch (ClassCastException ex){
ex.printStackTrace();
}
Double x = m1.getValue();
Double y = m2.getValue();
if (x < y)
return -1;
else if (x == y)
return 0;
else
return 1;
}
}
when I compile this program I get the following: 当我编译这个程序时,我得到以下内容:
warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map.Entry<java.lang.Integer,java.lang.Double>
m1 = (Map.Entry<Integer, Double>)o1;
I need to sort map entries on the basis of the Double Values. 我需要根据Double Values对映射条目进行排序。
If I create the following comparator then I get an error in the call to sort function of Arrays (I am getting an entry set from the map and then using the set as an array). 如果我创建了下面的比较器,那么在调用Arrays的sort函数时会出现错误(我从地图中获取一个条目集,然后使用set作为数组)。
public class ScoreComp implements Comparator<Map.Entry<Integer, Double>>
how to implement this scenario. 如何实现这种情况。
Assuming that you're using this comparator to sort a TreeMap
, then this isn't going to work. 假设您正在使用此比较器对TreeMap
进行排序,那么这不会起作用。 TreeMap
comparators are for comparing only the map keys, not the key->value entries. TreeMap
比较器仅用于比较映射键,而不是key-> value条目。 If your comparator needs access to the values, then it will have to look them up in the map itself, eg 如果你的比较器需要访问这些值,那么它必须在地图本身中查找它们,例如
final Map<Integer, Double> map = ....
public class ScoreComp implements Comparator<Integer> {
public int compare(Integer key1, Integer key2) {
Double x = map.getValue();
Double y = map.getValue();
if (x < y)
return -1;
else if (x == y)
return 0;
else
return 1;
}
}
edit: From your comments, I think your best option is to create a class that encapsulates the ID and the value, put those values into a List, and sort that. 编辑:从您的评论中,我认为您最好的选择是创建一个封装ID和值的类,将这些值放入List中,然后对其进行排序。
public class Item implements Comparable<Item> {
int id;
double value;
public int compareTo(Item other) {
return this.value - other.value;
}
}
and then 然后
List<Item> list = new ArrayList<Item>();
// ... add items here
Collections.sort(list);
Since Item
is itself Comparable
, you don't need an external Comparator
(unless you want one). 由于Item
本身是Comparable
,因此您不需要外部Comparator
(除非您需要)。
What's about rewriting as 什么是重写为
public class ScoreComp implements Comparator<Map.Entry<Integer, Double>> {
public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) {
if ( o1.getValue() < o2.getValue() ) return -1;
else if ( o1.getValue() == o2.getValue() ) return 0;
return 1;
}
}
stacker has described how to fix the code you've shown. stacker描述了如何修复你显示的代码。 Here's how to fix the code in your comment: First of all, don't use arrays, because arrays don't work with generics (you cannot have an array of a generic type). 以下是如何修复注释中的代码:首先,不要使用数组,因为数组不能使用泛型(您不能使用泛型类型的数组)。 Instead, you can use a List
and the Collections.sort()
method: 相反,您可以使用List
和Collections.sort()
方法:
List<Map.Entry<Integer, Double>> mList =
new ArrayList<Map.Entry<Integer, Double>>(Score.entrySet());
Collections.sort(mList, new ScoreComp());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.