Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java unchecked cast

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.

like image 578
Rohit Banga Avatar asked Feb 01 '10 12:02

Rohit Banga


People also ask

What is unchecked cast in Java?

2. What Does the “unchecked cast” Warning Mean? The “unchecked cast” is a compile-time warning. Simply put, we'll see this warning when casting a raw type to a parameterized type without type checking. An example can explain it straightforwardly.

How do I fix an unchecked cast warning?

You're using a raw type in your code, which is giving you the unchecked cast problem. You normally fix this by using a fully parameterized class instead: Comparable<Whatever>.

What does unchecked assignment mean in Java?

Unchecked assignment: 'java.util.List' to 'java.util.List<java.lang.String>' It means that you try to assign not type safe object to a type safe variable. If you are make sure that such assignment is type safe, you can disable the warning using @SuppressWarnings annotation, as in the following examples.


1 Answers

Assuming that you're using this comparator to sort a TreeMap, then this isn't going to work. TreeMap comparators are for comparing only the map keys, not the key->value entries. If your comparator needs access to the values, then it will have to look them up in the map itself, e.g.

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.

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).

like image 106
skaffman Avatar answered Oct 07 '22 05:10

skaffman