如何获取树图中最高值元素的 id

How to get the id of the highest value element in a treemap

我有一个包含一些值的树状图,即

TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
map.put(1,7);
map.put(6,3);  
map.put(3,18);
map.put(7,2);
map.put(12,42);

如何通过查找地图值中的最大值 (42) 来获取 ID (12)?

我们可以从 TreeMap 中获取 entrySet() 并对其进行流处理,然后通过比较每个条目的值并从条目中获取键来从条目集中找到最大值具有最高价值。

map.entrySet().stream()
              .max(Map.Entry.comparingByValue()))
              .ifPresent(e -> System.out.println(e.getKey()));