Map<String, ApprovedSaleData> dapartMap = approvedSaleDataList.stream() .collect(Collectors.toMap(ApprovedSaleData::getDepartment, item -> item)); 这段代码怎么防止空指针异常
时间: 2024-02-21 07:00:19 浏览: 56
可以在Lambda表达式中进行空指针判断。例如,可以使用Optional类的ofNullable方法将item包装成Optional对象,然后使用orElse方法指定默认值,以防止空指针异常。修改后的代码如下:
```
Map<String, ApprovedSaleData> dapartMap = approvedSaleDataList.stream()
.collect(Collectors.toMap(ApprovedSaleData::getDepartment, item -> Optional.ofNullable(item).orElse(new ApprovedSaleData())));
```
在上述代码中,如果item为null,则Optional.ofNullable(item)返回一个空的Optional对象。然后我们使用orElse方法指定了一个默认值new ApprovedSaleData(),以确保在item为null的情况下,也能够将一个默认值放入Map中。
相关问题
Map<String, ApprovedSaleData> dapartMap = approvedSaleDataList.stream() .collect(Collectors.toMap(ApprovedSaleData::getDepartment, item -> item)); 怎么避免getDepartment值为空报空指针
你可以在使用 `Collectors.toMap()` 方法之前,使用 `filter()` 方法过滤掉 `getDepartment()` 方法返回 `null` 值的元素。例如:
```
Map<String, ApprovedSaleData> dapartMap = approvedSaleDataList.stream()
.filter(item -> item.getDepartment() != null)
.collect(Collectors.toMap(ApprovedSaleData::getDepartment, item -> item));
```
这样就可以避免 `getDepartment()` 返回 `null` 值导致的空指针异常。
for (Winning winning : winnings) { // 若存在物流信息 发货时间不能为空 if (winning.getPostTime() != null){ rightNow.setTime(winning.getPostTime()); //日期加1个月 rightNow.add(Calendar.MONTH,1); flg = rightNow.getTime().compareTo(new Date()); if (flg <= 0 ){ flag = true; } } if (!VtuzxUtil.isEmpty(winning.getOutState())){ // 自动收货 if(winning.getOutState().equals(String.valueOf(PrizeOutStateEnum.Sent_ENUM.value())) && flag){ // 修改发放状态为已收货 winningDao.updOutStateById(String.valueOf(winning.getId()),PrizeOutStateEnum.Receive_Goods.value()); winning.setOutState(PrizeOutStateEnum.Receive_Goods.value()); flag = false; } } dataList.add( new VtuzxMap("prizeName",winning.getPrizeName()) .append("receiptNo",winning.getReceiptNo()) .append("companyName",winning.getCompanyName()) .append("department",winning.getDepartment()) .append("prizeNum",winning.getPrizeNum()) .append("outStore",winning.getOutStore()) .append("giveOut",winning.getGiveOut()) .append("userName",winning.getUserName()) .append("activityName",winning.getActivityName()) .append("outState",winning.getOutState()) .append("mobile",winning.getMobile()) .append("activityId",winning.getAwardActivityId()) .append("prizeType",winning.getPrizeType()) .append("clearingState",winning.getClearingState()) .append("postCompany",winning.getPostCompany()) .append("storeName", winning.getStoreName()) .append("postNo",winning.getPostNo()) .append("postName",winning.getPostName()) .append("contactMobile",winning.getContactMobile()) .append("province",winning.getProvince()) .append("city",winning.getCity()) .append("area",winning.getArea()) .append("createdAt",VtuzxUtil.formatYmdHmsF(winning.getCreatedAt())) .append("addressDetail",winning.getAddressDetail()) ); }使用stream优化
可以使用 Java 8 中的 stream 来优化上述代码,具体实现如下:
```
List<Map<String, Object>> dataList = winnings.stream()
.filter(winning -> winning.getPostTime() != null)
.map(winning -> {
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(winning.getPostTime());
rightNow.add(Calendar.MONTH, 1);
boolean flag = rightNow.getTime().compareTo(new Date()) <= 0;
if (!VtuzxUtil.isEmpty(winning.getOutState()) && winning.getOutState().equals(String.valueOf(PrizeOutStateEnum.Sent_ENUM.value())) && flag) {
winningDao.updOutStateById(String.valueOf(winning.getId()), PrizeOutStateEnum.Receive_Goods.value());
winning.setOutState(PrizeOutStateEnum.Receive_Goods.value());
flag = false;
}
return new VtuzxMap("prizeName", winning.getPrizeName())
.append("receiptNo", winning.getReceiptNo())
.append("companyName", winning.getCompanyName())
.append("department", winning.getDepartment())
.append("prizeNum", winning.getPrizeNum())
.append("outStore", winning.getOutStore())
.append("giveOut", winning.getGiveOut())
.append("userName", winning.getUserName())
.append("activityName", winning.getActivityName())
.append("outState", winning.getOutState())
.append("mobile", winning.getMobile())
.append("activityId", winning.getAwardActivityId())
.append("prizeType", winning.getPrizeType())
.append("clearingState", winning.getClearingState())
.append("postCompany", winning.getPostCompany())
.append("storeName", winning.getStoreName())
.append("postNo", winning.getPostNo())
.append("postName", winning.getPostName())
.append("contactMobile", winning.getContactMobile())
.append("province", winning.getProvince())
.append("city", winning.getCity())
.append("area", winning.getArea())
.append("createdAt", VtuzxUtil.formatYmdHmsF(winning.getCreatedAt()))
.append("addressDetail", winning.getAddressDetail());
})
.collect(Collectors.toList());
```
使用 stream 可以让代码更加简洁和易于理解,同时也有更好的可读性和可维护性。
阅读全文