static class EntryComparator implements
Comparator<Map.Entry<String, Integer>> {
public EntryComparator() {
}
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return o2.getValue().intValue() - o1.getValue().intValue();// 降序排列
}
}
@SuppressWarnings("unchecked")
public static Map<String, Integer> outputSortedHashMap(
Map<String, Integer> map) {
Map<String, Integer> tempMap = new LinkedHashMap<String, Integer>();
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
Collections.sort(list, new EntryComparator());
Iterator<Map.Entry<String, Integer>> i = list.iterator();
while (i.hasNext()) {
Map.Entry<String, Integer> entry = i.next();
tempMap.put(entry.getKey(), entry.getValue());
// System.out.println("key=" + entry.getKey() + ", value="+
// entry.getValue());
}
return tempMap;
}
hashmap按照value值排序实现
最新推荐文章于 2024-01-27 17:43:02 发布