Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取树映射中最高值元素的id_Java_Sorting_Treemap - Fatal编程技术网

Java 如何获取树映射中最高值元素的id

Java 如何获取树映射中最高值元素的id,java,sorting,treemap,Java,Sorting,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); TreeMap map=newtreemap(); 地图.put(1,7); 地图.put(6,3); 地图.put(3,18); 地图.put(7,2); 地图

我有一个带有一些值的树形图,即

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);
TreeMap map=newtreemap();
地图.put(1,7);
地图.put(6,3);
地图.put(3,18);
地图.put(7,2);
地图.put(12,42);

如何通过查找映射的值来搜索最高值(42)来获取ID(12)?

我们可以从
树映射中获取
entrySet()
,并在其上进行流式处理,然后通过比较每个条目的值并从具有最高值的条目中获取密钥,从条目集中找到最大值

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

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

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

您需要遍历所有条目。如果要按值搜索键,最好反转映射,因为映射对按键搜索值很有用。您需要遍历所有条目。如果要按值搜索键,最好反转贴图,因为贴图对按键搜索值很有用。