Which method will throw if there is no element in the stream?

  1. findFirst().orElse(null)
  2. findFirst().get()
  3. findAny().isPresent()
  4. findFirst().orElseThrow()
Correct answer findFirst().get()

Explanation

Calling `get()` on an empty Optional will throw NoSuchElementException.

Update: Admin added a tag on this question

Topics

By Admin · · 25 views