The first option is in fact a wrong answer. And it performs as in the explanation.
However,
Why the lambda in the first call to forEach() is not treated as a Function/UnuaryOperator instead of a Consumer since the String.toUpperCase() returns a String?
Sorry, not sure what you mean. What should not cause what to not compile? A method that returns void can call a method that returns anything within its body -
void m(){
methodThatReturnsSomething(); //no problem here. return value is ignored
}
But if a method expects a Consumer as argument, you can't pass it a Function (and vice versa).
So I am not sure what is the confusion.
If you like our products and services, please help us by posting your review here.
The first statement does convert each element to upper case. However, the new upper case value does not get back in the the list. It is lost. Therefore, the second statement still prints the lower case value.
Does the upper case value get lost because of String's immutability, right?