如何在 HashMap<Integer, Integer> 中找到最大的 VALUE(不是键)?
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));
您可以使用 hmap.values()
获取地图的值,然后使用 Collections.max
获取最大值
Collections.max(hmap.values());
如何找到这五个?
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));
您可以使用 hmap.values()
获取地图的值,然后使用 Collections.max
获取最大值
Collections.max(hmap.values());