java中List通过lamda按属性值进行groupby, 相关属性相加

14 篇文章 0 订阅
8 篇文章 1 订阅
VirtualFleetSettlementDetail virtualFleetSettlementDetail = new VirtualFleetSettlementDetail();
    virtualFleetSettlementDetail.setUserId(1L);
    virtualFleetSettlementDetail.setActivityId(1);
    virtualFleetSettlementDetail.setTeamId(1);
    virtualFleetSettlementDetail.setMemberGrowthValue(100);
    virtualFleetSettlementDetail.setSettlementId(1);

    VirtualFleetSettlementDetail virtualFleetSettlementDetail2 = new VirtualFleetSettlementDetail();
    virtualFleetSettlementDetail2.setUserId(1L);
    virtualFleetSettlementDetail2.setTeamId(1);
    virtualFleetSettlementDetail2.setActivityId(1);
    virtualFleetSettlementDetail2.setMemberGrowthValue(400);
    virtualFleetSettlementDetail2.setSettlementId(1);

    VirtualFleetSettlementDetail virtualFleetSettlementDetail4 = new VirtualFleetSettlementDetail();
    virtualFleetSettlementDetail4.setUserId(1L);
    virtualFleetSettlementDetail4.setTeamId(2);
    virtualFleetSettlementDetail4.setActivityId(1);
    virtualFleetSettlementDetail4.setMemberGrowthValue(300);
    virtualFleetSettlementDetail4.setSettlementId(1);

    VirtualFleetSettlementDetail virtualFleetSettlementDetail3 = new VirtualFleetSettlementDetail();
    virtualFleetSettlementDetail3.setUserId(2L);
    virtualFleetSettlementDetail3.setTeamId(1);
    virtualFleetSettlementDetail3.setActivityId(1);
    virtualFleetSettlementDetail3.setMemberGrowthValue(100);
    virtualFleetSettlementDetail3.setSettlementId(1);

    List<VirtualFleetSettlementDetail> pageList = new ArrayList<>();
    pageList.add(virtualFleetSettlementDetail);
    pageList.add(virtualFleetSettlementDetail2);
    pageList.add(virtualFleetSettlementDetail3);
    pageList.add(virtualFleetSettlementDetail4);
    List<VirtualFleetSettlementDetail> newpageList = new ArrayList<>();
    pageList.parallelStream()
        .collect(Collectors.groupingBy(o -> (o.getActivityId() + "" + o.getTeamId() + "" + o.getUserId() + "" + o.getSettlementId()), Collectors.toList()))
        .forEach(
            (id, transfer) -> {
              transfer.stream().reduce((a, b) -> new VirtualFleetSettlementDetail().setActivityId(a.getActivityId())
                  .setTeamId(a.getTeamId()).setUserId(a.getUserId()).setSettlementId(a.getSettlementId())
                  .setMemberGrowthValue(a.getMemberGrowthValue() + b.getMemberGrowthValue())).
                  ifPresent(newpageList::add);
            });
    System.out.println("newpageList==" + newpageList);

结果:


newpageList==[VirtualFleetSettlementDetail(activityId=1, settlementId=1, activityCode=null, teamId=2, userId=1, phone=null, captain=null, teamLevel=null, rightsType=null, memberGrowthValue=300, couponType=null, couponCode=null, couponTitle=null, couponSubTitle=null, couponConditionMoney=null, couponMoney=null, couponCount=null, couponIconUrl=null, status=null), VirtualFleetSettlementDetail(activityId=1, settlementId=1, activityCode=null, teamId=1, userId=1, phone=null, captain=null, teamLevel=null, rightsType=null, memberGrowthValue=500, couponType=null, couponCode=null, couponTitle=null, couponSubTitle=null, couponConditionMoney=null, couponMoney=null, couponCount=null, couponIconUrl=null, status=null), VirtualFleetSettlementDetail(activityId=1, settlementId=1, activityCode=null, teamId=1, userId=2, phone=null, captain=null, teamLevel=null, rightsType=null, memberGrowthValue=100, couponType=null, couponCode=null, couponTitle=null, couponSubTitle=null, couponConditionMoney=null, couponMoney=null, couponCount=null, couponIconUrl=null, status=null)]

 

[VirtualFleetSettlementDetail(activityId = 1, settlementId = 1, activityCode = null, teamId = 2, userId = 1, phone = null, captain = null, teamLevel = null, rightsType = null, memberGrowthValue = 300, couponType = null, couponCode = null, couponTitle = null, couponSubTitle = null, couponConditionMoney = null, couponMoney = null, couponCount = null, couponIconUrl = null, status = null), 
 VirtualFleetSettlementDetail(activityId = 1, settlementId = 1, activityCode = null, teamId = 1, userId = 1, phone = null, captain = null, teamLevel = null, rightsType = null, memberGrowthValue = 500, couponType = null, couponCode = null, couponTitle = null, couponSubTitle = null, couponConditionMoney = null, couponMoney = null, couponCount = null, couponIconUrl = null, status = null), 
 VirtualFleetSettlementDetail(activityId = 1, settlementId = 1, activityCode = null, teamId = 1, userId = 2, phone = null, captain = null, teamLevel = null, rightsType = null, memberGrowthValue = 100, couponType = null, couponCode = null, couponTitle = null, couponSubTitle = null, couponConditionMoney = null, couponMoney = null, couponCount = null, couponIconUrl = null, status = null)]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值