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
    评论
可以使用Java 8Stream API和Lambda表达式来获取List某个字段的值,并将其组成新的List集合。你可以使用map方法来映射每个对象的字段值,然后使用collect方法将这些值收集到一个新的List。 例如,如果你有一个List<Object>对象列表,并且你想要提取其一个字段的值,你可以使用以下代码: List<Object> objectList = new ArrayList<>(); List<Object> newList = objectList.stream() .map(Object::getVar) .collect(Collectors.toList()); 在这个例子,我们使用stream方法将List转换为一个,然后使用map方法将每个对象的getVar字段值映射到一个新的。最后,使用collect方法将这些值收集到一个新的List。 如果你有一个List<ShoppingCart>对象列表,并且你想要提取其一个字段的值,比如selectByUId的goodsId,你可以使用以下代码: List<ShoppingCart> selectByUId = new ArrayList<>(); List<Integer> goodsIdList = selectByUId.stream() .map(ShoppingCart::getGoodsId) .collect(Collectors.toList()); 在这个例子,我们使用stream方法将List转换为一个,然后使用map方法将每个对象的getGoodsId字段值映射到一个新的。最后,使用collect方法将这些值收集到一个新的List。 这样,你就可以使用Java 8的Stream API和Lambda表达式来获取List某个字段的值,并将其组成新的List集合。 #### 引用[.reference_title] - *1* *3* [Java8 stream 提取 List 元素的某一字段生成新的 List](https://blog.csdn.net/kjahe/article/details/126489216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [java list获取某个字段](https://blog.csdn.net/weixin_42594427/article/details/129448097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值