2016-12-02 19 views
-1

我有一個簡單的代碼,我將自定義類對象的列表轉換爲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]

編譯

不知道我在哪裏錯了。任何人都可以幫忙嗎?

+0

爲什麼人們會投票?給我一個理由。 – KayV

+0

不是我的失望,但可能是因爲你沒有向我們展示[mcve]。特別向我們展示'NPDto'中的獲取者的實際聲明。使用不正確的方法名稱會導致此錯誤。 –

+0

@ greg-449也添加了getter和setter。 – KayV

回答

1

您需要稍作修改,因爲分割返回String []

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

這就是我使用Arrays.asList將其轉換成列表 – KayV

+0

@KaranVerma這很有趣我採用了相同的代碼,它只編譯文件。 NumberUtils返回什麼?整數我假設? – Eugene

+0

是的,它返回整數。 – KayV

相關問題