I've tried to change this code to Java 8 streams. My code looks like this:
for(D d : n.getD()) {
for(M m : d.getT().getM()) {
if(m.getAC().contains(this)) {
return d;
}
}
}
and I want to convert it to java 8 streams. I've started like this:
n.getD().stream()
.map(m -> m.getT().getM())
but then I don't know if I should map again, or use a filter.
Other possible way is to use anyMatch
instead of second filter
return n.getD().stream().filter(
d -> d.getT().getM().stream().anyMatch(
m -> m.getAC().contains(this)
)
).findFirst(); // result will be Optional<D>
one way to handle this:
return n.getD().stream().filter(d -> d.getT().getM().stream().filter(m -> m.getAC().contains(this)).findFirst().isPresent()).findFirst();
in this case a null value is possible.
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