Java Stream - 编译时错误 - 类型与 Map<Integer,List<String>> 不匹配 : cannot convert from Map<Object, Object>

标签 java java-8 stream java-stream collectors

我有一个简单的代码,我将自定义类对象的列表转换为 map >。 代码如下:

List<NPDto> appList = new ArrayList<NPDto>(); 
//list gets populated though some other method

//Here is conerting code where i get compile time error
final Map<Integer, List<String>> appMap = appList.stream()
                                              .collect(
                                                Collectors.toMap(
                                                  np -> NumberUtils.toInt(np.getPId()),
                                                  np -> Arrays.asList(np.getAppsReceived().split(","))
                                              ));
// Here is my DTO                                              
public class NPDto {

    private String pId;
  private String appsReceived;

  public String getPId(){
    return pId;
  }

  public void setPId(String pId){
    this.pId = pId;
  }

  public String getAppsReceived(){
    return appsReceived;
  }

  public void setAppsReceived(String appsReceived){
    this.appsReceived = appsReceived;
  }
}

但是,我收到如下编译器错误:

Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>>

I am compiling with Java SE 8[1.8.0_91]

不知道哪里错了。有人可以帮忙吗?

最佳答案

您需要稍作更改,因为 split 返回一个 String []

np -> Arrays.asList(np.getAppsReceived().split(","))

关于Java Stream - 编译时错误 - 类型与 Map<Integer,List<String>> 不匹配 : cannot convert from Map<Object, Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40928236/

相关文章:

scala - 评估 2D 流时出现 OutOfMemoryError

javascript - 泵和管道有什么区别?

java - 无法完全模拟 RestHighLevelClient

java - 最小化 Java 函数调用开销

java - 从连接到 Android 的传感器(线程、异步任务、服务)连续获取数据

java - 使用 Optional 作为类中的属性是一种好习惯吗?

java - 使用流内的流检查进行过滤

objective-c - NSStream : Is there any airtight defense against blocking?

java - 缩小 Java 字段类型而不使封闭类通用?

Java Pattern 类没有公共(public)构造函数,为什么?