I have a comparator class in Java to compare Map entries:
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.
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).
public class ScoreComp implements Comparator<Map.Entry<Integer, Double>>
how to implement this scenario.