Robię tak, jak w kodzie poniżej, ale przy rzutowaniu na Entry wywala błąd (ClassCastException). Dlaczego? Jeśli tak, jak robię się nie da, to piszcie jak posortować HashMap po wartościach?
Dostaję nulla, po rzutowaniu na Entry<String, Integer>. Wie ktoś dlaczego?
HashMap<String, Integer> records;
Set<Entry<String,Integer>> set = records.entrySet();
Map.Entry<String,Integer>[] aRecords = set.toArray(aRecords);
Arrays.sort(aRecords,
new Comparator<Map.Entry<String, Integer>>() {
public int compare(
Map.Entry<String, Integer> e1,
Map.Entry<String, Integer> e2) {
if (e1.getValue() < e2.getValue())
return -1;
else if (e1.getValue() > e2.getValue())
return 1;
else
return 0;
}
});