簡體   English   中英

Java Stream-編譯時錯誤-類型不匹配:無法從Map轉換 <Object,Object> 到地圖 <Integer,List<String> &gt;

[英]Java Stream - Compile time Error - Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>>

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

不知道我在哪里錯。 有人可以幫忙嗎?

您需要稍作更改,因為split返回String []

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM