java unchecked cast

compiler-warnings, generics, java

Solution

stacker has described how to fix the code you've shown. 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<Map.Entry<Integer, Double>> mList = 
        new ArrayList<Map.Entry<Integer, Double>>(Score.entrySet()); 
    Collections.sort(mList, new ScoreComp());

Problem

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.

Original source

Related problems