I'm working/testing streams in Java8 and come across very frustrating issue.
I've got the code which compiles well:
List words = Arrays.asList("Oracle", "Java", "Magazine");
List wordLengths = words.stream().map((x) -> x.toUpperCase())
.collect(Collectors.toList());
And second one (nearly the same) which throw a warnings:
List words = Arrays.asList("Oracle", "Java", "Magazine");
List wordLengths = words.stream().map((x) -> {
x.toUpperCase();
}).collect(Collectors.toList());
Warning:
The method map(Function super String,? extends R>) in the type Stream is not applicable for the arguments (( x) -> {})
What does this additional brackets have changed?
解决方案
Your lambda expression returns a value. If you use brackets you need to add a return statement to your lambda function:
List words = Arrays.asList("Oracle", "Java", "Magazine");
List wordLengths = words.stream().map((x) -> {
return x.toUpperCase();
}).collect(Collectors.toList());