Selcuk Bor
Selcuk Bor

Reputation: 163

accessing HashMap data from a nested HashMap

I have an interesting situation here. I am building a board game and I have a board declared that is of HashMap

HashMap<String, HashMap> board = new HashMap<String, HashMap>();

There are multiple layers on this board, and this is why this HashMap must take other HashMaps.

We populate this board with tile objects, as following.

HashMap tileObject = new HashMap();
tileObject.put("key1", value1);
tileObject.put("key2", value2);
tileObject.put("name", value3);

We are adding this (and other tiles) to the board.

board.put((String)tileObject.get("name"), tileObject); 

So that's all good and dandy, we have added tiles to the board. Now my issue is, reading from this board. When analyzing the board I have this snippet of code in a function

HashMap takeTileObject = new HashMap();
takeTileObject.put("unique-coordinate", board.get("unique-coordinate");
// we are getting the values from board at key "unique-coordinate"

what I need to do is access key1/key2/name from takeTileObject. I have tried this

takeTileObject.get("unique-coordinate").(NOTHING HELPFUL HERE);

What I would ideally like to happen is something like this.

takeTileObject.get("unique-coordinate").get("key1");

is this possible?

Thank you greatly for any help in advance.

Upvotes: 1

Views: 1129

Answers (2)

Hanon
Hanon

Reputation: 3927

It is because didn't specify the type of takeTileObject

HashMap<String, HashMap> takeTileObject = new HashMap<String, HashMap>();

Tell the system that the value is a HashMap object and you should able to call it

takeTileObject.get("unique-coordinate").get("key1");

Upvotes: 3

Tino M Thomas
Tino M Thomas

Reputation: 1746

Change ths piece of code

HashMap(String, HashMap> board = new HashMap<String, HashMap>();

to

HashMap<String, HashMap> board = new HashMap<String, HashMap>();

Upvotes: 0

Related Questions