user3009924
user3009924

Reputation: 59

Hashmap in hash map try to insert something

This is my hashmap:

protected HashMap<String, HashMap<String, Player>> prisonsPlayers = new HashMap<String, HashMap<String, Player>>();

I try to insert something in it with:

prisonsPlayers.put(player.getWorld(), (HashMap<player.getName(), player>));

Erorr I get:

Multiple markers at this line
    - Syntax error, insert ")" to complete 
     Expression
    - Syntax error on token ")", invalid 
     Expression

I know I do something wrong but however I try, what ever I do I don't know how to insert that data to my hashmap.

Upvotes: 0

Views: 674

Answers (3)

NickJ
NickJ

Reputation: 9579

Prabhkaran is exactly right, has answer will work.

The code

HashMap<player.getName(), player>

is not valid, because you are supplying objects into the HashMap generic parameters, and they should be class definitions.

Example:

// Construct a HashMap with can contain Strings as keys and values
HashMap<String, String> mymap = new HashMap<String, String>();

// add Strings to my hashmap
mymap.put("hello","world");

is OK, but

// compilation failure!
new HashMap<"Hello","World">(); 

is not.

Upvotes: 0

JoeC
JoeC

Reputation: 1850

Syntax error (or maybe logic, too?)

prisonsPlayers.put(player.getWorld(), (HashMap<player.getName(), player>));

Try doing this

prisonsPlayers.put(player.getWorld(), (new HashMap<String, Player>()));
prisonsPlayers.get(player.getWorld()).put(player.getName(), player);

Upvotes: 0

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26104

This line

prisonsPlayers.put(player.getWorld(), (HashMap<player.getName(), player>));

Should be like this

Map<String, Player> map  = new HashMap<String, Player>();
map.put(player.getName(),player);  
prisonsPlayers.put(player.getWorld(), map);

Upvotes: 2

Related Questions