Additional brackets in Java8 stream map() function

馋奶兔 提交于 2020-01-30 02:56:30

问题


I'm working/testing streams in Java8 and come across very frustrating issue. I've got the code which compiles well:

        List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
    List<String> wordLengths = words.stream().map((x) -> x.toUpperCase())
        .collect(Collectors.toList());

And second one (nearly the same) which throw a warnings:

        List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
    List<String> wordLengths = words.stream().map((x) -> {
        x.toUpperCase();
    }).collect(Collectors.toList());

Warning:

The method map(Function<? super String,? extends R>) in the type Stream<String> is not applicable for the arguments ((<no type> x) -> {})

What does this additional brackets have changed?


回答1:


Your lambda expression returns a value. If you use brackets you need to add a return statement to your lambda function:

List<String> words = Arrays.asList("Oracle", "Java", "Magazine");
List<String> wordLengths = words.stream().map((x) -> {
    return x.toUpperCase();
}).collect(Collectors.toList());



回答2:


According to the official Oracle tutorial

A lambda expression consists of the following:

A comma-separated list of formal parameters enclosed in parentheses. The CheckPerson.test method contains one parameter, p, which represents an instance of the Person class.

Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter. For example, the following lambda expression is also valid:

p -> p.getGender() == Person.Sex.MALE 
    && p.getAge() >= 18
    && p.getAge() <= 25

The arrow token, ->

A body, which consists of a single expression or a statement block. This example uses the following expression:

p.getGender() == Person.Sex.MALE 
    && p.getAge() >= 18
    && p.getAge() <= 25

If you specify a single expression, then the Java runtime evaluates the expression and then returns its value. Alternatively, you can use a return statement:

p -> {
    return p.getGender() == Person.Sex.MALE
        && p.getAge() >= 18
        && p.getAge() <= 25;
}

A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:

email -> System.out.println(email)

Since there is only one parameter in the provided lambda expression (x) -> x.toUpperCase() we can omit the parentheses: x -> x.toUpperCase(). String#toUpperCase returns a new String so there is no need to use return statement and braces. If instead we had a complex block with return statements we would have to enclose it into braces. Moreover in this case it is better to use Method Reference String::toUpperCase

List<String> wordLengths = words.stream().map(String::toUpperCase).collect(Collectors.toList()); 


来源:https://stackoverflow.com/questions/46309032/additional-brackets-in-java8-stream-map-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!