KayV
KayV

Reputation: 13835

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

I hava a simple code where i am converting a List of my custom class object into a Map>. The code is as follows:

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;
  }
}

But, i am getting a compiler error as follows:

Type mismatch: cannot convert from Map<Object,Object> to Map<Integer,List<String>>

I am compiling with Java SE 8[1.8.0_91]

Don't know where i am wrong. Can anybody please help out?

Upvotes: 0

Views: 2991

Answers (1)

Eugene
Eugene

Reputation: 120848

you need to make a slight change as split returns a String [].

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

Upvotes: 1

Related Questions