java边界的list,java:不兼容类型:推理变量T具有不兼容的边界等式约束:下界: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.

public class Movie {

private String example;

private List movieTranses;

public Movie(String example, List 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 element in the lists:

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());

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

解决方案

The map call in

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

converts a Stream to a Stream>, which you can collect into a List>, not a List.

To get a single List, use flatMap :

List movieTransList1 =

movies.stream()

.flatMap(m -> m.getMovieTranses().stream())

.collect(Collectors.toList());

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值