Nested lists with streams in Java8

You can use two flatMap then a filter then you can pick the first one or if no result return null :

C c1 = listOfAObjects.stream()
        .flatMap(a -> a.getList().stream())
        .flatMap(b -> b.getPr().stream())
        .filter(c -> c.getName().equalsIgnoreCase(name))
        .findFirst()
        .orElse(null);

Leave a Comment