我有一个简单的代码,我将自定义类对象的列表转换为 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/