我有一个包含一些值的树状图,即
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);
如何通过查找 map 值中的最高值(42)来获取 ID(12)?
最佳答案
我们可以从 TreeMap
中获取 entrySet()
并在其上进行流式处理,然后通过比较每个条目的值来找到条目集中的最大值从具有最高值的条目中获取 key 。
map.entrySet().stream()
.max(Map.Entry.comparingByValue()))
.ifPresent(e -> System.out.println(e.getKey()));
关于java - 如何获取树形图中最高值元素的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443596/