I want to convert this piece code into streams
List<ECSUSU> listOfUSUs = msccRequest.getUsedServiceUnit();
if (listOfUSUs != null) {
ECSUSU usedServiceUnit = listOfUSUs.get(0);
if (usedServiceUnit.getCcTime() != null && usedServiceUnit.getCcTime() > 0) {
return UnitType.SECONDS;
} else {
return UnitType.BYTES;
}
}
I would thought this will work but I am getting NullPointerException. msccRequest.getUsedServiceUnit() might return null , that's why I need the check
msccRequest.getUsedServiceUnit().stream()
.filter(list -> list != null)
.limit(1)
.map(usedServiceUnit -> {
if (usedServiceUnit.getCcTime() != null && usedServiceUnit.getCcTime() > 0) {
return UnitType.SECONDS;
} else {
return UnitType.BYTES;
}
});