优雅的方式对一个大数组根据想要的值进行过滤为一个小数组——博主备忘

DBC 1.6K 0
短短代码,意义非凡,博主小惊叹!
    /**
     * 确认购物车商品信息
     * @param productIdList
     * @return
     */
    @Override
    public List<CartItemVO> confirmOrderCartItems(List<Long> productIdList) {

        //获取全部购物车的购物项
        List<CartItemVO> cartItemVOList =  buildCartItem(true);

        //根据需要的商品id进行过滤,并清空对应的购物项
        List<CartItemVO> resultList =  cartItemVOList.stream().filter(obj->{

            if(productIdList.contains(obj.getProductId())){
                this.deleteItem(obj.getProductId());
                return true;
            }
            return false;

        }).collect(Collectors.toList());

        return resultList;
    }
此例子为用户发起订单之后,查找全部购物车的商品,然后根据选择了的商品进行转换出来的新list,返回出来,并且对购物车里面的商品进行消除,可谓优雅至极!这里我们要学习的也就是Lambda 表达式了
根据需要的商品id进行过滤,并清空对应的购物项
List<CartItemVO> resultList =  cartItemVOList.stream().filter(obj->{

            if(productIdList.contains(obj.getProductId())){
                this.deleteItem(obj.getProductId());
                return true;
            }
            return false;

        }).collect(Collectors.toList());

发表评论 取消回复
表情 图片 链接 代码

分享