You can use flatMap :
response.getFirstNodes() .stream() .filter(first -> first.isValid()) .flatMap(first -> first.getSndNodes().stream()) .filter(snd -> snd.getType() == NodeType.AMOUNT) .mapToDouble(snd -> snd.getAmount()) .sum();
I am not sure if this break; intends in your source code.
Using the break; statement break; It should look like this:
response.getFirstNodes() .stream() .filter(first -> first.isValid()) .map(first -> first.getSndNodes().stream().filter(snd -> snd.getType() == NodeType.AMOUNT).findFirst()) .filter(Optional::isPresent) .mapToDouble(opt -> opt.get().getAmount()) .sum();
Basically, for each FirstNode you check whether it is valid, then you map each FirstNode to its SndNode , for which you will find the first one that has the type NodeType.AMOUNT . Then you need to filter to get only optional options that are not empty, and for them you get the SndNode in which they are contained, for which you get the corresponding amount.
source share