I am learning Java 8 and came across a situation. Where in I have to iterate over a list of strings and then convert them to upperCase. The possible solutions would be to stream the list. Among many suggestions from Intellij the below two seems to be useful.
list.stream()
.map(String::toUpperCase)
or
list.stream().
forEach(p -> p.toUpperCase())
I am confused on which one to use and the use cases for all the Suggestions. Can I get help regarding which method to use and how to understand using all those suggestions?
map()
does nothing without a terminal operation.forEach(p -> p.toUpperCase())
is a terminal operation that does nothing. If you're trying to convert a string to uppercase, you'll need to use the return value somehow, which neither snippet does.toUpperCase()
does not (cannot) modify the string to be uppercase. The method returns a new string with all uppercase letters, it doesn't update the existing string.