What does the error mean in my stream map?



Error:

 Error

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Jan 09 14:22:48 IST 2021 There was an unexpected error (type=Internal Server Error, status=500). Unresolved compilation problem: The method map(Function<? super Rating,? extends R>) in the type Stream is not applicable for the arguments (( rating) -> {}) java.lang.Error: Unresolved compilation problem: The method map(Function<? super Rating,? extends R>) in the type Stream is not applicable for the arguments (( rating) -> {})

at com.study.movie.controller.CatalogController.getCatalog(CatalogController.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at 

Error image

Code:

return ratings.stream()
                .map(rating -> {
                    Movie movie=restTemplate.getForObject("http://localhost:8081/movies/"+rating.getMovieId(), Movie.class);
                    new CatalogItem(movie.getName(),movie.getDescription(),rating.getRating());
        })
        .collect(Collectors.toList());

Answer

The code block in your lambda expression doesn’t return anything, but then .map() method requires a function / lambda that returns something.

You should write your code as

return ratings.stream()
           .map(rating -> {
               Movie movie=restTemplate.getForObject("http://localhost:8081/movies/"+rating.getMovieId(), Movie.class);
               return new CatalogItem(movie.getName(),movie.getDescription(),rating.getRating());
           })
           .collect(Collectors.toList());


Source: stackoverflow