java: incompatible types: inference variable T has incompatible bounds equality constraints: lower b

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

java:不兼容类型:推理变量T具有不兼容的边界等式约束:下限:java.util.List <>

 

我试图从流中获取一个列表,但我有一个报错Exception。

这是带有对象列表的Movie对象。

public class Movie {

    private String example;
    private List movieTranses;

    public Movie(String example, List movieTranses){
        this.example = example;
        this.movieTranses = movieTranses;
    }
    getter and setter

这是MovieTrans:

public class MovieTrans {

    public String text;

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

我在列表中添加元素:

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

我有这个编译错误:

Error:(44, 95) java: incompatible types: inference variable T has incompatible bounds
    equality constraints: MovieTrans
    lower bounds: java.util.List

经过分析发现: 

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

将a转换Stream为a Stream>,您可以将其转换为a ,而List>不是a List

要获得单个List,请使用flatMap

List movieTransList1 = 
    movies.stream()
          .flatMap(m -> m.getMovieTranses().stream())
          .collect(Collectors.toList());

你可能感兴趣的:(java)