用Lambda方法求Hashmap中Value的最大值

@用Lambda方法求Hashmap中Value的最大值

Map<String,Integer> map = new HashMap<>();
map.put(“1”, 1);
map.put(“2”, 2);
map.put(“3”, 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));
上一篇:fastAPI(2)--请求参数、路径参数、多body


下一篇:Fastapi学习总结(上)