虾米链接: http://2814704901.iteye.com/blog/1933740
public class sortMap {
public static void main(String[] args) {
Map<String, Integer> maps = new HashMap<String, Integer>();
maps.put("aa",2);
maps.put("bb",9);
maps.put("cc",7);
List<Map.Entry<String, Integer>> entryLists = new ArrayList<Map.Entry<String, Integer>>(maps.entrySet());
Collections.sort(entryLists, new EntryComparator());
// System.out.println(entryLists);for (Map.Entry<String, Integer> entry : entryLists) {
System.out.print(entry.getKey() + ":" + entry.getValue()+" ");
}
}public static class EntryComparator implements Comparator<Map.Entry<String, Integer>> {
// value列表顺序的比较器
public int compare(Map.Entry<String, Integer> map1, Map.Entry<String, Integer> map2) {
// 重写compare方法
return map1.getValue() - map2.getValue(); // 升序排列
// return map2.getValue() - map1.getValue();// 降序排列
}
}
}