public static void main(String[] args) {
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("001", 2);
map.put("002", 6);
map.put("003", 9);
map.put("004", 1);
map.put("005", 9);
List<Map.Entry<String, Integer>> orderList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(orderList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
// 降序:o2.getValue()-o1.getValue()
// 升序:o1.getValue()-o2.getValue()
return (o2.getValue()-o1.getValue());
}
});
for(Map.Entry<String, Integer> m : orderList){
System.out.println(m);
}
}
Map集合按照Value排序(Map value sort)
最新推荐文章于 2024-01-05 09:43:05 发布