侧边栏壁纸
  • 累计撰写 15 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

关于java stream 根据类型分组数据

天马
2021-03-13 / 0 评论 / 0 点赞 / 766 阅读 / 192 字 / 正在检测是否收录...
   public static void main(String[] args) {
        List<Map<String,ScheduleDTO>> map = new ArrayList<>();
        List<ScheduleDTO> list = new ArrayList<>();
        for (int i = 0; i < 2 ; i++) {
            for (int j = 0; j <4 ; j++) {
                ScheduleDTO scheduleDTO = new ScheduleDTO();
                scheduleDTO.setState(i+"");
                scheduleDTO.setType(j+"");
                list.add(scheduleDTO);
            }
        }
        Map<String, List<ScheduleDTO>> collect = list.stream().collect(Collectors.groupingBy(ScheduleDTO::getState, Collectors.toList()));

        System.out.println(JSONObject.toJSONString(collect));
    }
}

分组的数据

{
    "0":[
        {
            "state":"0",
            "type":"0"
        },
        {
            "state":"0",
            "type":"1"
        },
        {
            "state":"0",
            "type":"2"
        },
        {
            "state":"0",
            "type":"3"
        }
    ],
    "1":[
        {
            "state":"1",
            "type":"0"
        },
        {
            "state":"1",
            "type":"1"
        },
        {
            "state":"1",
            "type":"2"
        },
        {
            "state":"1",
            "type":"3"
        }
    ]
}
0

评论区