3

I am working on a text based game in java and would like to use a python like dictionary for the map, the HashMap doesn't really work for me since i want to have multiple values linked to the one dictionary e.g.

In python you can define a dictionary like this:

room_x = { "name": "X",

"description":"Random description",

"exits": {linked to another dictionary},

"items": list_of_players_items}

Is something similar possible in Java?

Thanks in advance.

Markwin
  • 177
  • 2
  • 16
  • Any reason you don't create a class with fields, like a good little Object-Oriented programmer using an Object-Oriented language? – Andreas Nov 21 '16 at 05:25
  • Please try a few Google searches before asking a new question, especially for common tasks. – TigerhawkT3 Nov 21 '16 at 05:25
  • 1
    @TigerhawkT3 just FYI I wasn't specifically asking for a HashMap solution, but asking how to replicate the dictionary function from python in java(Hoping for a better solution than HashMap). The fact that HashMap solution turned out to be the best way to do it, doesn't make it the same question as the one you tagged. – Markwin Nov 21 '16 at 05:38
  • This question isn't the same as the linked one - it just contains duplicate information. If, during your research, you had come across that page, it would have fully resolved the issue. That's what duplicates mean. :) – TigerhawkT3 Nov 21 '16 at 05:42
  • @TigerhawkT3 based on what you said, nobody should ask a question here, they can always find answers related to their specific problems:) – Haifeng Zhang Nov 21 '16 at 05:45
  • @HaifengZhang - Not _nobody_ - it's very desirable for people facing new problems that they cannot resolve with thorough research to ask new questions. But yes, if five seconds of Googling would have answered a question, ___of course___ a new question isn't needed. :) – TigerhawkT3 Nov 21 '16 at 05:48
  • @TigerhawkT3 please stop down-voting my other answers on this site. – Haifeng Zhang Nov 21 '16 at 06:08
  • @HaifengZhang - I vote a lot around here, to provide input on whether I think a question or answer is useful (just like everyone else does). I cannot say whether I have voted on any of your posts (as voting is anonymous), but I can say that I am unable to give anyone special treatment as you request. – TigerhawkT3 Nov 21 '16 at 06:44

2 Answers2

3

HashMap can do the work:

HashMap<String, String> hashMap = new HashMap<>();  //<key,value>  both key and value are Strings
hashMap.put("name", "X");
hashMap.put("description", "Random description");

To link multiple values. you need to change the key value pair types as

HashMap<String,HashMap<String, String>> linkedMap = new HashMap<>();  // key is string and value is HashMap<String, String>
HashMap<String,String> itemsMap = new HashMap<>();
itemsMap.put("item1", "this is first item");
itemsMap.put("item2", "this is second item");
linkedMap.put("items", itemsMap);

Above code is just a quick example I wrote, you need to declare the real type of your data, it doesn't have to be String.
Hope it helps.

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0

HashMap or Hashtable is enough. Both have key-value pairs, where key is a String and value can be any object. So you can define it like HashMap<String, Object> map = new HashMap<String, Object>().

The value can be anything (even null for HashMap not Hashtable).

So you can have another HashMap as the value, which solves "exits": {linked to another dictionary}

And a List as the value, which solves "items": list_of_players_items

This gives difference between HashMap and Hashtable.

Sample code :

List players = // some List
HashMap exits = // some Map
HashMap<String, Object> room_x = new HashMap<String, Object>();
map.put("name",name_string);
map.put("desc",desc_string);
mp.put("exits",exits);
map.put("items",players);
Community
  • 1
  • 1
Ramanujan R
  • 1,601
  • 2
  • 23
  • 43