使用java8新特性Optinal优化这些代码public BaseusResponse<BatchOperateResultVO> updRetailOrAdjDate(OrderUpdDateBatchCmd updDateBatchCmd) { List<Object> successList = new ArrayList<>(); List<Object> failList = new ArrayList<>(); List<BatchOperateResultVO.BatchOperateResultDetailVO> batchOperateResultDetailVoList = new ArrayList<>(); updDateBatchCmd.getList().forEach(updCmd -> { if (!OrderRetailConstants.CAN_UPD_DATE_ORDER_TYPE_LIST.contains(updCmd.getOrderType())) { throw new OrderBusinessException("订单类型不允许修改"); } }); // 按类型分组 Map<Integer, List<OrderUpdDateCmd>> typeAndCmdListMap = updDateBatchCmd.getList().stream().collect(Collectors.groupingBy(OrderUpdDateCmd::getOrderType)); List<OrderUpdDateCmd> retailCmdList = typeAndCmdListMap.get(OrderCenterTypeEnum.RETAIL_ORDER_TYPE.getOrderType()); List<OrderUpdDateCmd> retailAfterCmdList = typeAndCmdListMap.get(OrderCenterTypeEnum.RETAIL_AFTER_ORDER_TYPE.getOrderType()); List<OrderUpdDateCmd> adjList = typeAndCmdListMap.get(OrderCenterTypeEnum.ADJUSTMENT_ORDER_TYPE.getOrderType()); if (!CollectionUtils.isEmpty(retailCmdList)) { orderRetailUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList, failList, batchOperateResultDetailVoList, retailCmdList); } if (!CollectionUtils.isEmpty(retailAfterCmdList)) { orderRetailAfterSalesUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList, failList, batchOperateResultDetailVoList, retailAfterCmdList); } if (!CollectionUtils.isEmpty(adjList)) { orderBusinessAdjustmentUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList, failList, batchOperateResultDetailVoList, adjList); } return BaseusResponse.success(BatchOperateResultVO.builder().totalCount(successList.size() + failList.size()).successCount(successList.size()).failCount(batchOperateResultDetailVoList.size()).batchOperateResultDetailVO(batchOperateResultDetailVoList).build()); }
时间: 2024-02-05 21:04:41 浏览: 79
JAVA8的新特性学习笔记
下面是使用Java 8的Optional优化后的代码:
```java
public BaseusResponse<BatchOperateResultVO> updRetailOrAdjDate(OrderUpdDateBatchCmd updDateBatchCmd) {
List<Object> successList = new ArrayList<>();
List<Object> failList = new ArrayList<>();
List<BatchOperateResultVO.BatchOperateResultDetailVO> batchOperateResultDetailVoList = new ArrayList<>();
updDateBatchCmd.getList().forEach(updCmd -> {
if (!OrderRetailConstants.CAN_UPD_DATE_ORDER_TYPE_LIST.contains(updCmd.getOrderType())) {
throw new OrderBusinessException("订单类型不允许修改");
}
});
// 按类型分组
Map<Integer, List<OrderUpdDateCmd>> typeAndCmdListMap = updDateBatchCmd.getList().stream()
.collect(Collectors.groupingBy(OrderUpdDateCmd::getOrderType));
Optional.ofNullable(typeAndCmdListMap.get(OrderCenterTypeEnum.RETAIL_ORDER_TYPE.getOrderType()))
.ifPresent(retailCmdList -> orderRetailUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList, failList,
batchOperateResultDetailVoList, retailCmdList));
Optional.ofNullable(typeAndCmdListMap.get(OrderCenterTypeEnum.RETAIL_AFTER_ORDER_TYPE.getOrderType()))
.ifPresent(retailAfterCmdList -> orderRetailAfterSalesUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList,
failList, batchOperateResultDetailVoList, retailAfterCmdList));
Optional.ofNullable(typeAndCmdListMap.get(OrderCenterTypeEnum.ADJUSTMENT_ORDER_TYPE.getOrderType()))
.ifPresent(adjList -> orderBusinessAdjustmentUpdateCmdExe.updDate(updDateBatchCmd.getUpdDate(), successList, failList,
batchOperateResultDetailVoList, adjList));
return BaseusResponse.success(BatchOperateResultVO.builder()
.totalCount(successList.size() + failList.size())
.successCount(successList.size())
.failCount(batchOperateResultDetailVoList.size())
.batchOperateResultDetailVO(batchOperateResultDetailVoList)
.build());
}
```
上述代码中,我们使用了`Optional.ofNullable()`方法来处理可能为空的`List`,并通过`ifPresent()`方法传入一个Lambda表达式,在Lambda表达式中执行对应的操作。这样可以避免`null`值的判断和多层嵌套的代码。同时,这种写法也使得代码更加简洁易读。
阅读全文