我有一個簡單的代碼,我將自定義類對象的列表轉換爲Map> 。 的代碼如下:Java Stream - 編譯時錯誤 - 類型不匹配:無法從Map <Object,Object>轉換爲Map <整數,列表<String>>
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]
不知道我在哪裏錯了。任何人都可以幫忙嗎?
爲什麼人們會投票?給我一個理由。 – KayV
不是我的失望,但可能是因爲你沒有向我們展示[mcve]。特別向我們展示'NPDto'中的獲取者的實際聲明。使用不正確的方法名稱會導致此錯誤。 –
@ greg-449也添加了getter和setter。 – KayV