I used this method to sorting my hash function. When I compile the program, these errors appear:
Note: Retriever.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
My hashMap<String, Double>
private static Map sortByComparator(Map unsortMap) {
List list = new LinkedList(unsortMap.entrySet());
// sort list based on comparator
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue())
.compareTo(((Map.Entry) (o1)).getValue());
}
});
// put sorted list into map again
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
That is a compiler warning because you are ignoring the generics and using “raw” types. Ypu need to specify the generics as follows:
What’s going on here, is that by specifying the generic types you’re telling the compiler what types of objects these collections contain. Because of this I’ve been able to eliminate all the casts in the comparator and second loop. This makes the method actually type safe and checkable by the compiler.
What the compiler is telling you with the warning is that because you’re using raw types and casting it can’t check the accuracy of your typing. The other option is to simply suppress this warning using @SuppressWarnings but it’s better to actually make the method type safe.