我有一个简单的代码,我将自定义类对象的List转换为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>>
我正在使用Java SE 8 [1.8.0_91]
进行编译
不知道我哪里错了。有人可以帮忙吗?
答案 0 :(得分:1)
你需要稍作改动,因为split会返回 String [] 。
np -> Arrays.asList(np.getAppsReceived().split(","))