将Map中的元素按值的大小进行排序
/**
* Map 按值(value)排序
* @param map
* @return Map.Entry[]
*/
public static Map.Entry[] getSortedHashtableByValue(Map<String, Integer> map) {
Set set = map.entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
Arrays.sort(entries, new Comparator() {
public int compare(Object arg0, Object arg1) {
Long key1 = Long.valueOf(((Map.Entry) arg0).getValue().toString());
Long key2 = Long.valueOf(((Map.Entry) arg1).getValue().toString());
return key1.compareTo(key2);
}
});
return entries;
} |
|