'convert from java6 to java8 using stream and filter [closed]
I have the below code and need to convert to java8 using stream and filter.Can someone please help?
for (CertEntity entity : listOfExpiredCert){
if (entity!=null && entity.getCertVldToDt()!=null){
if (entity.getCertSerNum()!=null && entity.getCertSerNum().equals(baseObject.getCertSerNum())){
continue;
}
else{
if (entity.getCertVldToDt()!=null && entity.getCertVldToDt().compareTo(baseDate)>0){
entityWithLatestDate = entity;
}
else if (entity.getCertVldToDt()!=null && entity.getCertVldToDt().compareTo(baseDate)<0){
entityWithLatestDate = baseObject;
}
}
}
}
Solution 1:[1]
if I understand correctly then you are just trying to find CertEntity with the latest CertVldToDt and matching CertSerNum to some baseObject. This code should do it:
entityWithLatestDate = listOfExpiredCert.stream()
.filter(Objects::nonNull)
.filter(entity -> entity.getCertSerNum() != null)
.filter(entity -> entity.getCertVldToDt() != null)
.filter(entity -> entity.getCertSerNum().equals(baseObject.getCertSerNum()))
.filter(entity -> entity.getCertVldToDt().compareTo(baseDate) > 0)
.max(Comparator.comparing(CertEntity::getCertVldToDt))
.orElse(baseObject);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LordSkin |
