英文:
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<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);
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());
专注分享java语言的经验与见解,让所有开发者获益!
评论