Java小知识:使用Lambda表达式,判断字段非空后进行求和

阅读 171

2022-01-27

使用方式如下:

//定义集合
List<PrescriptionDTO> entryValue = entry.getValue();

//数据处理
BigDecimal sum = entryValue.stream().map(PrescriptionDTO::getMoney).
filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

函数拆解分析:
1. map(PrescriptionDTO::getMoney) ----获取集合里对象的指定字段
2. filter(Objects::nonNull) ----只获取money字段非空的对象
3. reduce(BigDecimal.ZERO, BigDecimal::add) ----对money字段进行求和

精彩评论(0)

0 0 举报