I'm trying to sort a ConcurrentSkipListMap by value in Java, here is my code :
ConcurrentSkipListMap<String,Float> cslMap = new ConcurrentSkipListMap(new Comparator() {
public int compare(Object o1,Object o2) {
return ((Comparable)((Map.Entry)(o1)).getValue()).compareTo(((Map.Entry)(o2)).getValue());
}
});
cslMap_Map.put("B",0.2f);
cslMap_Map.put("A",0.1f);
cslMap_Map.put("C",1f);
Got an error message when compiling it:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map$Entry
What's the correct way to do it ?
Thanks for the answers, but in it's Java doc, it says "The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time", so how to supply it with a Comparator that sort by its values ?