I\'m trying to refactor the following code to lambda expressions with `stream, especially the nested foreach loops:
public static Result match (Response rsp) {
Look at flatMap:
flatMap(Function super T,? extends Stream extends R>> mapper)
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.
Code sample assuming isValid()
doesn't throw
Optional sndNode = rsp.getFirstNodes()
.stream()
.flatMap(firstNode -> firstNode.getSndNodes().stream()) //This is the key line for merging the nested streams
.filter(sndNode -> sndNode.isValid())
.findFirst();
if (sndNode.isPresent()) {
try {
parse(sndNode.get());
} catch (ParseException e) {
lastex = e;
}
}