Stream实战-统计求和

Stream实战-统计

stream在开发中经常使用场景就是统计,再次记录一下实际开发中用的到统计,使用模拟数据。

需求如下:

在这里插入图片描述

代码如下:

/**
 * map集合统计
 */
public class StreamDemo4 {
   
    /**
     * 实体类
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class Book{
   
        /** 名称 */
        private String name;
        /** 数量 */
        private Integer count;
    }

    /**
     * 初始化集合
     */
    public List<Book> init(){
   
         return Stream.of(
            new Book("java",10),
            new Book("java",20),
            new Book("web",10),
            new Book("linux",10)
        ).collect(Collectors.toList());
    }

    /**
     * map分组统计每科书的数量
     */
    public Map<String,Integer> mapCount(){
   
        List<Book> init = init();
        return init.stream().
            collect(Collectors.groupingBy(Book::getName, Collectors.summingInt(Book::getCount)));
    }

    /**
     * Map 转换 List
     */
    public List<Book> mapConvertList(){
   
        Map<String, Integer> map = mapCount();
        return map.entrySet().stream()
            .map(entry -> new Book(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
    }


    /**
     * list统计每科书的数量
     */
    public List<Book> listCount(){
   
        List<Book> init = init();
        return init.stream().collect(Collectors.groupingBy(Book::getName))
            .entrySet().stream().map(
                entry -> {
   
                    String name = entry.getKey();
                    int sum = entry.getValue().stream().mapToInt(Book::getCount).sum();
                    return new Book(name, sum);
                }).collect(Collectors.toList());
    }

    public List<Book> groupAndSum() {
   
        List<Book> init = init();
        return init.stream()
            .collect(Collectors.groupingBy(Book::getName,
                Collectors.reducing(0, Book::getCount, Integer::sum)))
            .entrySet().stream()
            .map(entry -> new Book(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
    }

    public static void main(String[] args) {
   
        StreamDemo4 streamDemo4 = new StreamDemo4();

        System.out.println("=== ===Map统计=== ===");
        streamDemo4.mapCount().entrySet().forEach(System.out::println);

        System.out.println("=== ===Map转换List=== ===");
        streamDemo4.mapConvertList().forEach(System.out::println);

        System.out.println("=== ===List统计=== ===");
        streamDemo4.listCount().forEach(System.out::println);

        System.out.println("=== ===List统计方式2=== ===");
        streamDemo4.groupAndSum().forEach(System.out::println);
    }
}

代码中的方法

  • groupingBy:对流进行分组,在此案例中把name当作Key,把List<Book》当作value
  • entrySet:把map集合转换成Set<Map<String,Integer》》格式
  • map:提取原流中元素 进行处理
  • mapToInt:把结果转换成IntStream流
  • sum:和mapToInt搭配使用,IntStream流的结果求和
  • reducing:对流进行一些统计,如求和,求积,统计,最大,最小等
    进行处理
  • mapToInt:把结果转换成IntStream流
  • sum:和mapToInt搭配使用,IntStream流的结果求和
  • reducing:对流进行一些统计,如求和,求积,统计,最大,最小等
  • summingInt:对整数流元素进行求和

相关推荐

  1. 统计素数并求和

    2024-01-28 10:56:01       51 阅读
  2. 数列求和统计输入正数个数 题目

    2024-01-28 10:56:01       33 阅读
  3. Flink Stream API实践

    2024-01-28 10:56:01       28 阅读
  4. Spark-Streaming+HDFS+Hive实战

    2024-01-28 10:56:01       52 阅读
  5. <span style='color:red;'>Stream</span>

    Stream

    2024-01-28 10:56:01      32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-28 10:56:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-28 10:56:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-28 10:56:01       87 阅读
  4. Python语言-面向对象

    2024-01-28 10:56:01       96 阅读

热门阅读

  1. 初识Cargo-Rust的包管理器

    2024-01-28 10:56:01       60 阅读
  2. 轻松将Word文档转换为PDF:R语言实战教程

    2024-01-28 10:56:01       61 阅读
  3. 面试 HTML 框架八股文十问十答第一期

    2024-01-28 10:56:01       70 阅读
  4. 300. 最长递增子序列(动态规划)

    2024-01-28 10:56:01       59 阅读
  5. 关系运算和逻辑运算

    2024-01-28 10:56:01       51 阅读
  6. 1.27学习总结

    2024-01-28 10:56:01       46 阅读
  7. 第八章 对象、类与面向对象编程 第四节——类

    2024-01-28 10:56:01       42 阅读
  8. 代码随想录算法训练营|day17

    2024-01-28 10:56:01       72 阅读
  9. OpenCV 1 - 加载 显示 修改 保存图像

    2024-01-28 10:56:01       48 阅读
  10. 文旅游戏的多元应用场景

    2024-01-28 10:56:01       57 阅读