Java 流 - 编译时错误 - 类型不匹配:无法从 Map<Object,Object> 转换为 Map<Integer,List<String>>
Java Stream - Compile time Error - Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>>
我有一个简单的代码,我将我的自定义 class 对象列表转换为 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 returns a String [].
np -> Arrays.asList(np.getAppsReceived().split(","))
我有一个简单的代码,我将我的自定义 class 对象列表转换为 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 returns a String [].
np -> Arrays.asList(np.getAppsReceived().split(","))