Optional类的使用
程序员文章站
2022-03-04 10:51:14
...
对多层的dto中的数据进行操作时,需要进行判空处理时,使用optional来进行判空,简化代码
Optional.ofNullable(bankCard).map(b->b.getQueryBindCardsResponseDto()).ifPresent(bindCard->{
Optional.ofNullable(bindCard.getDefaultCard()).map(d->d.getCardNo()).ifPresent(cardNo->{
try{
bankCard.getQueryBindCardsResponseDto().getDefaultCard().setCardNo(DESUtil.aesEncrypt(cardNo, DESUtil.KEY));
}catch (Exception e){
log.info("支付工具卡号加密异常,异常",e);
}
});
Optional.ofNullable(bindCard.getEnableCardList()).filter(list-> !list.isEmpty()).ifPresent(enableCardList->{
enableCardList.forEach(record->{
try{
record.setCardNo(DESUtil.aesEncrypt(record.getCardNo(), DESUtil.KEY));
}catch (Exception e){
log.info("支付工具卡号加密异常,异常",e);
}
});
});
log.info("没有问题");
Optional.ofNullable(bindCard.getDisableCardList()).filter(list-> !list.isEmpty()).ifPresent(disableCardList->{
disableCardList.forEach(record->{
try{
record.setCardNo(DESUtil.aesEncrypt(record.getCardNo(), DESUtil.KEY));
}catch (Exception e){
log.info("支付工具卡号加密异常,异常",e);
}
});
});
});
下一篇: Java8新特性_Optional类