如何在 HashMap 中找到最大的值(而不是键)?

huangapple go评论103阅读模式
英文:

How to find the biggest VALUE (not a key) in a HashMap<Integer, Integer>?

问题

如何找到这个五?

HashMap<Integer, Integer> hmap = new HashMap<Integer, Integer>();
hmap.put(98, 3);
hmap.put(-120, 2);
hmap.put(12, 5);
hmap.put(344, 1);
hmap.put(-220, 1);

我尝试了这个,但它不喜欢我的 hmap。

System.out.println(Collections.max(hmap));
英文:

How to find the five?

HashMap&lt;Integer, Integer&gt; hmap = new HashMap&lt;Integer, Integer&gt;();
hmap.put(98, 3);
hmap.put(-120, 2);
hmap.put(12, 5);
hmap.put(344, 1);
hmap.put(-220, 1);

I tried this but it doesn't like my hmap.

System.out.println(Collections.max(hmap));

答案1

得分: 3

你可以使用 hmap.values() 获取映射的值,然后使用 Collections.max 来获取最大值。

Collections.max(hmap.values());
英文:

You can get the value of map using hmap.values() then use Collections.max to get max value

Collections.max(hmap.values());

huangapple
  • 本文由 发表于 2020年7月24日 02:19:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63060703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定