假设我在数据库的表中有以下记录。
这里Emplid和date的组合就是我的PK。 对于每次更改,我都会维护记录版本。 (在最后 3 行中,共有 3 个版本作为数量的变化)。
现在我想获取最新版本的 emplid = 1111 date = 28-02-2019 的记录(用户不知道数据库中有多少版本)。
我怎样才能用 lambda 表达式做到这一点。
HrcKey matchedKey = hrcKeyList.stream()
.filter(x -> (currEmplid.equals(x.getEmplid()) &&
format.format(updateListFromUI.getDate())
.equals(format.format(x.getDate()))))
.findFirst()
.orElse(null);
这会给我 vesrion 1。 但我怎样才能获得最新版本。
最佳答案
您可以使用max
通过比较version
值来获取最新的对象。
hrcRestatementKeyList.stream()
.filter(x -> (currEmplid.equals(x.getEmplid()) &&
format.format(updatedHrcDbpRestatement.getExtr_effdt())
.equals(format.format(x.getExtr_effdt()))))
.max(Comparator.comparingInt(HrcRestatementKey::getVersion))
.orElse(null);
关于Java 8 : Lambda expression with where condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62408150/