filter在过滤时,使用方法里面返回的true和false进行判断。
具体例子如下:
// 对比两个列表
List<UpdateEntity> updateEntityList;
List<AnotherEntity> anotherEntityList;
List<UpdateEntity> updateList = updateEntityList.stream().filter(e -> {
for (AnotherEntity anotherEntity : anotherEntityList) {
if (!e.getUserId().equals(anotherEntity.getUserId())) {
continue;
}
if (!e.getEffectiveFrom().equals(anotherEntity.getEffectiveFrom())
|| !e.getEffectiveTo().equals(anotherEntity.getEffectiveTo())) {
return true;
}
}
return false;
}).collect(Collectors.toList());
如上面例子所示,当filter里面返回是true时,会保留当前在判断的对象;当返回是false时,则会将其过滤掉。