0

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?

9
  • Why you people are down voting? Give me a reason. Commented Dec 2, 2016 at 9:52
  • Not my downvote but probably because you haven't shown us a minimal reproducible example. In particular show us the actual declarations of the getters in NPDto. Using incorrect method names can give this error. Commented Dec 2, 2016 at 9:55
  • @greg-449 Added the getter and setter also. Commented Dec 2, 2016 at 10:01
  • 2
    You are missing a right parenthesis (the one that closes the collect method call). If it's a typo in your question, then include which compiler you are using (javac / Eclipse's compiler / ...), knowing that the latter may have problems with type inference. Compiles fine for me using javac 1.8.0_102. Commented Dec 2, 2016 at 10:02
  • 1
    .. then include which compiler you are using. Commented Dec 2, 2016 at 10:10

1 Answer 1

1

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

np -> Arrays.asList(np.getAppsReceived().split(","))
Sign up to request clarification or add additional context in comments.

4 Comments

That is what i am converting this into a List using Arrays.asList
@KaranVerma that's funny I took the same code you have and it compiles just file. What does NumberUtils return? an Integer I assume?
Yes it returns Integer.
I guess, its commons.apache NumberUtils.toInt, designed to make erroneous code even worse by silently converting null and empty strings to zeros…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.