I am unable to understand how to remove the below null check by using Java 8 Optional
for (A objA : listOfObjectsA) {
if (objA.getStringField() == null) continue;
// some code to do if not null
}
if "some code to do if not null" only operates on objA.getStringField() then you can do:
listOfObjectsA.stream()
.map(A::getStringField)
.filter(Objects::nonNull)
.forEach(e -> ...);
However, if you still want to have access to the A elements then as the other answers have shown you have no choice but to perform an explicit objA.getStringField() != null:
listOfObjectsA.stream()
.filter(a -> a.getStringField() != null)
.forEach(a -> ...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With