【JAVA】java中将一个list进行拆解重新组装

发布于:2025-02-11 ⋅ 阅读:(80) ⋅ 点赞:(0)

一、使用场景

1、当需要对一个list中的元素属性进行重新赋值,比如一个list中存储了订单数据,我们需要改变list中每个订单的id,然后再重新输出订单list

      if(CollectionUtils.isNotEmpty(orderList)){
            orderList.forEach(p->{
                PointsExchangeOrderItem item = new PointsExchangeOrderItem();
                item.setOrderId(p.getId());
                List<PointsExchangeOrderItem> itemList = item.select();
                if (CollectionUtils.isNotEmpty(itemList)){
                    List<String> collects = itemList.stream().map(PointsExchangeOrderItem::getExchangeId).collect(Collectors.toList());
                    String exchangeIdStr = String.join(",", collects);
                    p.setExchangeInPointsId(exchangeIdStr);
                    p.setExchangeOutPointsId(exchangeIdStr);

                }


            });
        }

        return orderList;

      if(CollectionUtils.isNotEmpty(orderList)){
            orderList.forEach(p->{
                PointsExchangeOrderItem item = new PointsExchangeOrderItem();
                item.setOrderId(p.getId());
                List<PointsExchangeOrderItem> itemList = item.select();
                if (CollectionUtils.isNotEmpty(itemList)){
                    List<String> collects = itemList.stream().map(PointsExchangeOrderItem::getExchangeId).collect(Collectors.toList());
                    String exchangeIdStr = String.join(",", collects);
                    p.setExchangeInPointsId(exchangeIdStr);
                    p.setExchangeOutPointsId(exchangeIdStr);

                }


            });
        }

        return orderList;


网站公告

今日签到

点亮在社区的每一天
去签到