使用Optinal.ofNullable 会很耗时吗
时间: 2024-01-05 15:19:09 浏览: 114
根据提供的引用内容,使用`Optional.ofNullable`不会很耗时。`Optional.ofNullable`是Java 8中的一个方法,它可以将一个可能为null的对象包装成一个`Optional`对象。它的作用是在代码中避免出现空指针异常。使用`Optional.ofNullable`的时间复杂度是O(1),因此不会很耗时。
相关问题
使用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()); }
下面是使用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`值的判断和多层嵌套的代码。同时,这种写法也使得代码更加简洁易读。
阅读全文