Additional brackets in Java8 stream map() function

后端 未结 2 686
一个人的身影
一个人的身影 2021-01-15 22:51

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

        List words = Arra         


        
2条回答
  •  梦毁少年i
    2021-01-15 23:25

    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());
    

提交回复
热议问题