Optional takeWhile dropWhile Challenge

Optional takeWhile dropWhile

The Optional concept is present in many other programming languages. The main goal of the Optional class is to avoid NullPointerException. It’s much easier to deal with null values when we use the concepts of an Optional. In this challenge, we will also explore the takeWhile and dropWhile methods from Java 9. Therefore you will be fixing your knowledge with Optional, takeWhile, and dropWhile!

Are you prepared to have fun with this Java Challenge and refine your Java skills? Take 5 minutes of your time and get this challenge done!

It's time to improve your Java skills with this Optional TakeDropWhile Challenge

Optional TakeDropWhile Challenge

What will happen in the following code? Optional takeWhile

That’s it challenger, rock on! Keep taking action and relentlessly break your limits! Don’t hesitate to leave a comment with a question if anything is not clear!

Written by
Rafael del Nero
Join the discussion

3 comments
    • The list is not ordered!

      So after the “takeWhile” operation you are left only with 10. 1 is not higher then 5 and operation stops there.
      Then the “dropWhile” operation drops all what is left – so that 10 gets removed as its higher then 9.

      So the stream is empty and the value in “or” is returned.

  • The sample isn’t the best here – if you use one of the “or” operators for returning an alternative value and you know what the concrete value is (777 in the example), then you shouldn’t wrap it in the optional but just use the “orElse” so its not wrapped unnecessary.

Stand Out as a Java Developer and Get Your Dream Job!

You will get the book by email in a few minutes after registering.