获取一个list数组查到比amount小的所有数据,然后进行过滤去重,根据金额进行排序,取到查出的list数据中amount最大的一条数据。
List<RechargePromotionSnapshot> rechargePromotionSnapshotList = rechargePromotionSnapshotDao.findByExample(example);
//收款金额
int amount = accountTransactionReqVo.getAmount().intValue();
Optional<RechargePromotionSnapshot> rechargePromotionSnapshot = rechargePromotionSnapshotList.stream()
.filter(m -> m.getRechargeAmount() < amount)
.sorted(Comparator.comparingInt((RechargePromotionSnapshot m) -> m.getRechargeAmount()).reversed())
.findFirst();
思路:
1.先用filter进行过滤(去重)
2.sort方法进行根据int类型进行排序(第一条的amount最小)
3.排序之后用reversed方法进行颠倒
4.再用findFirst即可取到最后一条
注意:
.sorted(Comparator.comparingInt((RechargePromotionSnapshot m) -> m.getRechargeAmount()).reversed())
使用sorted进行排序之后接着用reversed进行倒叙时,容易出现找不到类的错误,原因是排序之后再倒叙,倒叙后的list不知道自己是属于哪个类,所以要在排序的对象前加一个类名 (RechargePromotionSnapshot m) 这样
之后用isPresent判断是否存在值,进行接下来的业务逻辑操作。
//判断是否存在值
if (rechargePromotionSnapshot.isPresent()) {
//获取过滤出来的数据用.get()方法
rechargePromotionSnapshot.get();
//业务逻辑操作
}