[英]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.