jdk8 的新特性list中stream流进行分组并合并字段

3 篇文章 0 订阅

原始数据

字段1字段2
A2
A3
A4
B4

想要达到的效果数据

字段1字段2
A9
B4

通过字段1进行分组然后合并字段2的数据值

通过 jdk8 的新特性list中stream流进行分组并合并字段

//获取查询条件下视图的数据,即原始数据集合
        List<VResPortUse> vResPortLists = new ArrayList<>();
        if (!CollectionUtils.isEmpty(nes)) {
            vResPortLists = neManager.getPortReportInfo(portReportDO);
        }
//放到新的list集合中去
        List<VResPortUse> newVResPortLists = new ArrayList<>();

//按照网元进行分组后计算被用端口和端口总数
            vResPortLists.parallelStream()
                    .collect(Collectors.groupingBy(item -> (item .getNeId()), Collectors.toList()))
                    .forEach((id, transfer) -> {
                        transfer.stream()
                                .reduce((a, b) ->
                                        new VResPortUse(a.getNeId(),
                                                a.getPortTypeId(),
                                                a.getPortTypeName(),
                                                a.getTotal() + b.getTotal(),
                                                a.getUsed() + b.getUsed(),
                                                a.getNeName(),
                                                a.getPortTypeId(),
                                                a.getNeTypeName(),
                                                a.getNeTypeCategoryId(),
                                                a.getNeTypeCategoryName(),
                                                a.getCorpId(),a.getCorpName(),
                                                a.getSubnetPath(),a.getParentSubnetId(),
                                                a.getParentSubnetName()
                                        ))
                                .ifPresent(newVResPortLists::add);
                    });

实体类

@Data
public class VResPortUse {
    public VResPortUse() {
    }

    public VResPortUse(Long neId, Integer portTypeId, String portTypeName, Long total, Long used, String neName, Integer neTypeId, String neTypeName, Integer neTypeCategoryId, String neTypeCategoryName, Integer corpId, String corpName, String subnetPath, Long parentSubnetId, String parentSubnetName) {
        this.neId = neId;
        this.portTypeId = portTypeId;
        this.portTypeName = portTypeName;
        this.total = total;
        this.used = used;
        this.neName = neName;
        this.neTypeId = neTypeId;
        this.neTypeName = neTypeName;
        this.neTypeCategoryId = neTypeCategoryId;
        this.neTypeCategoryName = neTypeCategoryName;
        this.corpId = corpId;
        this.corpName = corpName;
        this.subnetPath = subnetPath;
        this.parentSubnetId = parentSubnetId;
        this.parentSubnetName = parentSubnetName;
    }

    /**
     * 所属网元id(查询辅助列)
     */
    private Long neId;

    /**
     * 端口类型
     */
    private Integer portTypeId;

    /**
     * 端口类型名称
     */
    private String portTypeName;

    private Long total;

    private Long used;

    /**
     * 网元名称
     */
    private String neName;

    /**
     * 网元型号
     */
    private Integer neTypeId;

    /**
     * 网元型号名称
     */
    private String neTypeName;

    /**
     * 网元型号分类id
     */
    private Integer neTypeCategoryId;

    /**
     * 描述信息
     */
    private String neTypeCategoryName;

    /**
     * 厂商id,自增长主键
     */
    private Integer corpId;

    /**
     * 厂商名称
     */
    private String corpName;

    /**
     * 从顶层子网开始的路径(子网与子网之间以.隔开),禁止其他表使用
     */
    private String subnetPath;

    /**
     * 所在子网id,0代表顶层子网
     */
    private Long parentSubnetId;

    private String parentSubnetName;

}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值