【问题标题】:How to find the biggest VALUE (not a key) in a HashMap<Integer, Integer>?如何在 HashMap<Integer, Integer> 中找到最大的 VALUE(不是键)?
【发布时间】:2020-07-23 18:19:26
【问题描述】:

如何找到五个?

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));

【问题讨论】:

标签: java hashmap max element


【解决方案1】:

您可以使用hmap.values() 获取地图的值,然后使用Collections.max 获取最大值

Collections.max(hmap.values());

【讨论】:

  • 如果某个答案解决了您的问题,请尝试将其标记为已接受。这将有助于未来的访客。您还可以选择投票。
  • 您可能需要等待几天才能决定接受答案,因为在此期间可能会发布更多答案。最终接受答案是一种很好的风格(当然,除非所有答案都是 cr*p,但例如,这个答案很好)。
  • 同意等待时间的概念。它可能有助于邀请更好/改进的答案。作为旁注:接受的答案也可能不被接受,以支持更好的答案。
猜你喜欢
  • 1970-01-01
  • 2015-02-19
  • 1970-01-01
  • 2013-06-09
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
相关资源
最近更新 更多