Map<String,Integer> map = new HashMap<>();
map.put("张三", 1);
map.put("李四", 2);
map.put("王五", 3);
//第一种方法
Optional<Map.Entry<String, Integer>> max0 = map.entrySet()
.stream()
.max(Map.Entry.comparingByValue());
//第二种方法
Optional<Map.Entry<String, Integer>> max1 = map.entrySet()
.stream().max((x1, x2) -> Integer.compare(x1.getValue(), x2.getValue()));
//第三种方法
Optional<Map.Entry<String, Integer>> max3 = map.entrySet()
.stream()
.collect(Collectors.maxBy(Map.Entry.comparingByValue()));
//第四种方法
Optional<Map.Entry<String, Integer>> max4 = map.entrySet()
.stream()
.max(Comparator.comparingInt(Map.Entry::getValue));
//第五种方法
IntSummaryStatistics max5 = map.entrySet()
.stream()
.collect(Collectors.summarizingInt(Map.Entry::getValue));
java的lambda求map最大值的五种方法
最新推荐文章于 2024-05-29 18:17:55 发布