对Map进行排序

   //对Map进行排序
@SuppressWarnings("unchecked")
public static Map sortByValue(Map map) {
// 将键值的entryset放到链表中
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
// 将链表按照值得从大到小进行排序
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o2)).getValue()).compareTo(((Map.Entry) (o1)).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}

你可能感兴趣的:(对Map进行排序)