Java: incompatible types: T output variable has incompatible equality constraints: lower bounds: java.util.List <>

I am trying to get a list from a stream, but I have an exception.

Here is a Movie object with a list of the object.

 public class Movie { private String example; private List<MovieTrans> movieTranses; public Movie(String example, List<MovieTrans> movieTranses){ this.example = example; this.movieTranses = movieTranses; } getter and setter 

Here is the MovieTrans:

 public class MovieTrans { public String text; public MovieTrans(String text){ this.text = text; } getter and setter 

i add the item to the lists:

 List<MovieTrans> movieTransList = Arrays.asList(new MovieTrans("Appel me"), new MovieTrans("je t'appel")); List<Movie> movies = Arrays.asList(new Movie("movie played", movieTransList)); //return a list of MovieTrans List<MovieTrans> movieTransList1 = movies.stream().map(Movie::getMovieTranses).collect(Collectors.toList()); 

I have this compiler error:

 Error:(44, 95) java: incompatible types: inference variable T has incompatible bounds equality constraints: MovieTrans lower bounds: java.util.List<MovieTrans> 
+5
source share
2 answers

Calling map in

 movies.stream().map(Movie::getMovieTranses) 

converts a Stream<Movie> to Stream<List<MovieTrans>> , which you can put together in List<List<MovieTrans>> , not List<MovieTrans> .

To get one List<MovieTrans> , use flatMap :

 List<MovieTrans> movieTransList1 = movies.stream() .flatMap(m -> m.getMovieTranses().stream()) .collect(Collectors.toList()); 
+10
source

Expression type List<List<MovieTrans>> : This is a concatenation of the results of the getMovieTranses method.

Use flatMap :

 List<MovieTrans> movieTransList1 = movies.stream() .flatMap(m -> m.getMovieTranses().stream()) .collect(Collectors.toList()); 
+8
source

Source: https://habr.com/ru/post/1262987/


All Articles