Java 8 nested streams : return a value in last stream

baskets.stream()
            .flatMap(basket -> basket.getItems().stream())
            .filter(item -> item.equals("someValue"))
            .findAny()
            .orElseThrow(NoSuchElementException::new);

The advantage of using findAny instead of findFirst is that findFirst doesn't work with parallel streams. Therefore, if you want to parallelize the above operation all you'll need to do is replace the stream() method with parallel()


  1. Use flatMap to get ride of nested lists, extract each List<Item> and merge them into a Stream<Item>, it acts like all substream were merged together.
  2. Use filter to ignore the non matching elements.
  3. Use findFirst to get the first occurence only and stop processing
  4. Use orElseThrow to throw an exception if no occurence of someValue were found.

Here you go

public class SomeService {
    public int findValueInBatch(Batch batch) {
        return batch.getBaskets().stream()
            .flatMap(basket -> basket.getItems().stream())
            .filter(item -> item.getProperty.equals("someValue"))
            .findFirst()
            .orElseThrow(() -> new IllegalArgumentException("value not found"));
    }
}

To eliminate both loops you can use flatMap to produce a Stream<Item> of all the Items of all the Baskets :

return batch.getBaskets()
            .stream()
            .flatMap(b -> b.getItems().stream())
            .filter(item -> item.getProperty.equals("someValue"))
            .findFirst()
            .orElse(some default value); // using .get() would throw an exception
                                         // if no match is found