Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I used this code snippet to find the first item in the String List which contained 'o' character :

List<String> words = Arrays.asList("hi", "hello", "bye", "goodbye");
String s1 = words.stream().filter(word -> word.contains("o")).findFirst().orElse(null);

So this finds the first item that has 'o' (hello) and if it doesn't find anything it will return null as specified by orElse(null). So my question is :

Is it really necessary to use orElse(null) and if I don't specify it what will it return as default if it doesn't find any matches?

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.