map集合是有3个元素,{苹果,价格=50},{华为,价格=80},{vivo,价格=30},这怎么排序,求daima
想要的效果打印{华为,价格=80},{苹果,结果=50},{vivo,价格=30}

JAVA中的map怎么根据值排序
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 兴趣互联 2022-04-13 09:48关注
public static void main(String[] args) { Map<String, Integer> maps = new HashMap<>(); maps.put("iphone",50); maps.put("hua_wei",80); maps.put("vivo",30); LinkedList<Map.Entry<String, Integer>> list = new LinkedList<>(maps.entrySet()); Comparator<Map.Entry<String, Integer>> comparator = Comparator.comparing(Map.Entry::getValue);Collections.sort(list, comparator.reversed()); list.stream().forEach(t->{ System.out.println(t.getKey()+":"+t.getValue()); }); }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报