简体   繁体   English

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

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

i try to get a list from a stream but i have an exception. 我试图从流中获取一个列表,但我有一个例外。

Here is the Movie object with a list of an object. 这是带有对象列表的Movie对象。

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: 这是MovieTrans:

public class MovieTrans {

    public String text;

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

i add the element in 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>

The map call in map电话

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

converts a Stream<Movie> to a Stream<List<MovieTrans>> , which you can collect into a List<List<MovieTrans>> , not a List<MovieTrans> . Stream<Movie>转换为Stream<List<MovieTrans>> ,您可以将其收集到List<List<MovieTrans>> ,而不是List<MovieTrans>

To get a single List<MovieTrans> , use flatMap : 要获取单个List<MovieTrans> ,请使用flatMap

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

The type of your expression is List<List<MovieTrans>> : it's the concatenation of the results of the getMovieTranses method. 表达式的类型是List<List<MovieTrans>> :它是getMovieTranses方法结果的串联。

Use flatMap instead: 请改用flatMap

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:不兼容的类型:推理变量 T 具有不兼容的边界等式约束:下限:java.util.List&lt;&gt; 用于添加字段 - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> for adding field 不兼容的类型:推理变量T具有不兼容的边界等式约束:捕获#1的? 扩展java.lang.Object - incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object java EnumSet,不兼容的类型:推断变量E具有不兼容的范围 - java EnumSet, incompatible types: inference variable E has incompatible bounds java:不兼容的类型:推断变量 RR 的边界不兼容 - java: incompatible types: inference variable RR has incompatible bounds Java实体组件系统-推断变量T具有不兼容的范围 - Java Entity Component System - Inference variable T has incompatible bounds 错误:不兼容的类型:推断变量R具有不兼容的界限(Lambda Java 8) - error: incompatible types: inference variable R has incompatible bounds (Lambda java 8) Java “错误:不兼容的类型:推理变量 E 的边界不兼容”错误 - Java "error: incompatible types: inference variable E has incompatible bounds" error 推断变量T具有不兼容的范围错误 - inference variable T has incompatible bounds Error 推断变量T具有不兼容的范围 - inference variable T has incompatible bounds 错误:不兼容的类型:推断变量R具有不兼容的范围 - error: incompatible types: inference variable R has incompatible bounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM