optional ofNullable filter

Soprano ofNullable stream Challenge

Since Java 9, it’s possible to use Optional with a stream in case we need to manipulate values from a List. In this Java Challenge, we will explore the use of a stream in an Optional! Are you ready to solve this Java Challenge?

[qsm quiz=22]

If you want to watch the FULL video explanation, check this out: (Before, try out the Java Challenge on your own)

This is it, keep breaking your limits and never stop improving your skills!

Written by
Rafael del Nero
Join the discussion

3 comments
  • Hi Rafael,

    I am curios to know what if we have List and some of element of list is null.
    How to make use of Stream.ofNullable in this case to check null element?

    • List sopranoList = new ArrayList();
      Collections.addAll(sopranoList, null, new Soprano(List.of(“hi”, “hello”)));

    • Got it.
      List sopranoList = new ArrayList();
      Collections.addAll(sopranoList, null, new Soprano(List.of(“hi”, “hello”, “bye”)));

      /** */
      sopranoList.stream()
      .flatMap(e -> Stream.ofNullable(e)) //empty or Stream of single element sopnaro
      .flatMap(e -> Stream.of(e.list)) //Stream of list of string
      .flatMap(s -> s.stream()) //Stream of string
      .filter(ele -> ele.equals(“hello”))
      .forEach(System.out::println);

Senior Java Interview Questions from Global Companies

Includes detailed answers and practical explanations from real interviews.

Delivered to your email within minutes.