Stream实战-统计求和

Stream实战-统计

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

需求如下:

代码如下:

java 复制代码
/**
 * 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:对整数流元素进行求和
相关推荐
C雨后彩虹1 分钟前
CAS 在 Java 并发工具中的应用
java·多线程·并发·cas·异步·
范纹杉想快点毕业12 分钟前
嵌入式系统架构之道:告别“意大利面条”,拥抱状态机与事件驱动
java·开发语言·c++·嵌入式硬件·算法·架构·mfc
2501_9403152617 分钟前
【无标题】2390:从字符串中移除*
java·开发语言·算法
半聋半瞎20 分钟前
Flowable快速入门(Spring Boot整合版)
java·spring boot·后端·flowable
散峰而望29 分钟前
【算法竞赛】树
java·数据结构·c++·算法·leetcode·贪心算法·推荐算法
毕设源码-邱学长37 分钟前
【开题答辩全过程】以 基于SpringBoot的理工学院学术档案管理系统为例,包含答辩的问题和答案
java·spring boot·后端
shejizuopin40 分钟前
基于SSM的高校旧书交易系统的设计与实现(毕业论文)
java·mysql·毕业设计·论文·ssm·毕业论文·高校旧书交易系统的设计与实现
修己xj1 小时前
SpringBoot解析.mdb文件实战指南
java·spring boot·后端
咩图1 小时前
Sketchup软件二次开发+Ruby+VisualStudioCode
java·前端·ruby
我命由我123451 小时前
Android 开发问题:Duplicate class android.support.v4.app.INotificationSideChannel...
android·java·开发语言·java-ee·android studio·android-studio·android runtime