Java8用法

场景1: 一个仓库管理员管理多个仓库 筛选出仓库列表
List<String> warehouseCodes = baseWarehouseRoleEntityList.stream().map(item->item.getWarehouse_id().toString()).collect(Collectors.toList());

若是不想集合中有重复的 可以转化为set
Set<String> lg_schedule_ids = lgStockUpDetailEntityList.stream().map(item -> item.getLg_schedule_id() + "").collect(Collectors.toSet());

附带计算费用
第一种计算方式
Double totalFee = lgBillSettlementEntityList.stream().map(LgBillSettlementEntity::getFee).reduce(0.0, Double::sum);
第二种计算方式
Double totalNumber = saleOrder.getGoods_list().stream().mapToDouble(SaleOrderGoodsDto::getGood_num).sum();




附带前端传的json数组循环
for (Iterator iterator = jsonArray.iterator(); iterator.hasNext();) {
                    JSONObject jsonObject = (JSONObject) iterator.next();
                    String biz_type = jsonObject.getString("biz_type");

}

附带工厂模式发送异步线程请求
CompletableFuture.runAsync(() -> {
                        String result = restTemplate.postForObject(callBackUrl,inDto,String.class);
                    });
场景2: 对一个列表进行循环 如对计划列表循环
            if (list.size() > 0) {
                //zbId 主表自增ID
                list.forEach((BizDeclarationPlanGoodsEntity planGoods) -> {
                    planGoods.setId(zbId);
                });
            }
场景3: 对一个对象进行set值的时候不能允许为null,或者某些地方不准有空指针异常。
如下面代码statisticsEntity,代表的是去年某段时间发货情况。
可能没有发货量 数据库查询为null。 
因为getGood_num()返回是String,所以必须要Double.valueof一下转为Double类型,但是如果返回为null,会报错,因此Optional方法就派上用场。
            ​           prodPlanDto.setSale_num(Double.valueOf(Optional.ofNullable(statisticsEntity).map(s->s.getGood_num()).orElse("0.00")));

场景4:如何查询某个商品的库存,你传入商品编码列表调用库存接口,返回商品列表。
由于可能来自于不同仓库,会出现一个商品编码出现在不同的商品实体类里面,因此需要对商品编码进行分组求和。
Map<String, Double> inventoryGoodsMap = goodsList.stream().collect(Collectors.groupingBy(item -> item.getGoods_code(), HashMap::new, Collectors.summingDouble(Goods::getGoods_quantity)));

场景5:倒减功能 有的时候循环的最后一次不能直接算 需要拿总量减去前面已经相加的 这样合计才不会出现少了一分或者少一点的情况

public class IterablesUtils {
    public static <T> void forEach(Integer startIndex,Iterable<? extends T> elements, BiConsumer<Integer, ? super T> action) {
        Objects.requireNonNull(elements);
        Objects.requireNonNull(action);
        for (T element : elements) {
            action.accept(startIndex++, element);
        }
    }
}

IterablesUtils.forEach(0,prodPlanGoodsEntities,(indexGoods,itemGoods)->{
                if (indexGoods==prodPlanGoodsEntities.size()-1){
                    Double var0 = sum_cal_confirm_quantity[0];
                    itemGoods.setConfirm_quantity(ArithUtils.sub(confirm_quantity,var0));
                }else {
                    Double plan_quantity = itemGoods.getPlan_quantity();
                    Double cal_confirm_quantity = ArithUtils.mul(ArithUtils.div(plan_quantity,plan_quantity_sum),confirm_quantity);
                    itemGoods.setConfirm_quantity(cal_confirm_quantity);
                    sum_cal_confirm_quantity[0]=ArithUtils.add(sum_cal_confirm_quantity[0],cal_confirm_quantity);
                }
            });

场景6:如需要对单据状态按照明细id按照最新排序 即最新的放上面
        Collections.sort(list, Comparator.comparing(BillStatusEntity::getAutoid).reversed());

场景7:需要对一个list进行循环处理 且处理完成以后被另外一个list接收
 // 批量插入模版选项
            List<PrintTmplOptionEntity> printTmplOptionList = printDefaultOptionList.stream().map(item -> {
                PrintTmplOptionEntity printTmplOption = new PrintTmplOptionEntity();
                printTmplOption.copyFrom(item);
                printTmplOption.setId(null);
                printTmplOption.setPt_id(printTmplSetting.getId());
                return printTmplOption;
            }).collect(Collectors.toList());

场景8:分组
   // 库存商品按照仓库和编码分组
        Map<String, Goods> inventoryGoodsMap = inventoryGoodsList.stream()
                .collect(Collectors.toMap(item -> item.getWarehouse_id() + "#" + item.getGoods_code(), item -> item));

  // 请求中的商品按照仓库和商品编码分组求和,算出每个仓库商品的总预订单数量
        Map<String, Double> requestGoodsGroupSumMap = requestSaleGoodsList.stream().collect(Collectors.groupingBy(item -> item.getWarehouse_erp_pk() + "#" + item.getGoods_code(),
                HashMap::new, Collectors.summingDouble(ReserveDeliveryOrderDetailEntity::getGood_num)));

场景9: 交集 而且是多个list进行取交集 不是简单的取交集 
/**
     * 取多个集合的交集 注意交集对象要重写equals和hashcode
     * @param elementLists
     * @return
     */
    public List<T> retainElementList(List<List<T>> elementLists) {

        Optional<List<T>> result = elementLists.parallelStream()
                .filter(elementList -> elementList != null && ((List) elementList).size() != 0)
                .reduce((a, b) -> {
                    a.retainAll(b);
                    return a;
                });
        return result.orElse(new ArrayList<T>());
    }

场景10:多个任务并行执行


​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值