You doubt to save a String in HashMap

0

I have a little problem..I'm using a HashMap > to save values. But in certain cases I have to store a String instead of a List.

Is there a way to do some cast or something like that without having to create another Map?

Part of the code is as follows:

HashMap<String,List<Object>> message = new HashMap<>(); message.put("lista de juegos",user.getMyJuegos(IdPlayer)); message.put("error","Please try again later");

Where user.getMyJuegos (IdPlayer) returns a List with all the games of a player. And the error message is in case the player is not in the BD. The problem occurs in the line of the error message and is as follows: "no suitable method found for put (java.lang.String, java.lang.String)"

Thank you very much from now.

    
asked by Weeknd23 25.02.2018 в 14:01
source

1 answer

0

You can not save a string directly since the Map declaration expects a list, what you have to do is treat that string as one more list. Just insert that string in a new list and save it in the map:

message.put("error",Arrays.asList("Please try again later"));

And to recover the value, do it as with any list:

message.get("error").get(0);
    
answered by 25.02.2018 / 15:23
source