系统运维系列 之Java中对一个List对象做分组操作

1.首先定义一个LIst对象

List<TemplateBusinessDO> templateBusinessDOList = new List<TemplateBusinessDO>();

2.执行分组操作

Map<Integer, List<TemplateBusinessDO>> groupBusinessCollection = templateBusinessDOList.stream().collect(Collectors.groupingBy(TemplateBusinessDO::getId));

3.如果只是分组已经完成,如果想对分组数据做合并操作可以枚举分组后的LIst

for (Map.Entry<Integer, List<TemplateBusinessDO>> entryBusinessItem : groupBusinessCollection.entrySet()) {
            List<TemplateBusinessDO> entryUserList = entryBusinessItem.getValue();
 
            TemplateBusinessDO tempItem = entryUserList.get(0);
 
            entryUserList.forEach((item) -> {
                tempItem.setContent(1 + "," + 2);
            });
 
            tempTemplateBusinessList.add(tempItem);
     }

参考资料:
https://blog.csdn.net/xiaoguang44/article/details/127764523 Java中对一个List对象做分组操作

你可能感兴趣的:(java,算法)