I am attempting to sort a hashMap by value, I have a method
public void sortByValues(HashMap inMap, HashMap<Character, Float> outMap) {
List list = new LinkedList(inMap.entrySet());
// Defined Custom Comparator here
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
}
});
// Here I am copying the sorted list in HashMap
// using LinkedHashMap to preserve the insertion order
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
outMap.put(entry.getKey(), entry.getValue());
}
}
The keys are chars, and the values are floats. However I cannot enter the sorted keys/values into the outMap because entry.getKey() is of type Object. How can I change this so that it puts a Key(char) and Value(float) into the outMap rather than both being objects?