8

In JavaScript, you can declare all the keys and value combinations for a JSON object in one go as follows...

var myJSON = {
    'key1' : 'value 1',
    'key2' : 'value 2',
    'key3' : 'value 3',
    'key4' : 'value 4',
    'key5' : 'value 5',
    'key6' : 'value 6'
};

I was wondering whether we can do something similar in Java for HashMaps.

HashMap<String, String> map = new HashMap<String, String>(
    "Key 1", "Value 1",
    "Key 2", "Value 2",
    "Key 3", "Value 3"
);

Basically, I need this as a one time read only thing as Config for the other parts of my code. I searched and tried a few solutions, but was not able to make them work. Or is there a better approach that I can do?

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Shahid Thaika
  • 2,133
  • 5
  • 23
  • 59

2 Answers2

8

Though {{ (double brace) is an anti pattern, something like this you can write

HashMap<String, String> map = new HashMap<String, String>(){{
put("Key 1", "Value 1");
put("Key 2", "Value 2");
put("Key 3", "Value 3");    
}};

Edit :

If you are looking for a better way to put elements, iterate over your json and put key values in a loop.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 4
    @JordiCastilla or not! The map will hold a reference to the enclosing class which may be undesirable... – assylias Sep 01 '15 at 14:12
  • 1
    just missing a quote in `"Value 3` .... ;) – Jordi Castilla Sep 01 '15 at 14:12
  • @assylias my opinion... but this is the way OP is asking, I like more your example, yes, but OP is asking for a static way similar to JSon... and this is the way... – Jordi Castilla Sep 01 '15 at 14:13
  • I did ask if there was a better solution at the end. I guess this is a solution, but it may be preferable to go with assylias' reply, yes? Basically, this is just a one time read only variable thing. I will not be manipulating it. So would prefer the most efficient method. – Shahid Thaika Sep 01 '15 at 14:42
  • @ShahidThaika you can freely choose your answer :) – Suresh Atta Sep 01 '15 at 14:58
  • I meant to confirm this - This reply is the solution to my question, but from a programming standpoint, it may be better to code using assylias' reply. In other words, choose this as an answer, but code using the other. – Shahid Thaika Sep 01 '15 at 15:01
  • @ShahidThaika My answer will be different if you ask for `what is the best way to add elements`, My answer purely concentrated on `Literal declaration of HashMap in Java` :) – Suresh Atta Sep 01 '15 at 15:04
  • Can you add that too, then I will close by choosing this reply. – Shahid Thaika Sep 01 '15 at 15:20
  • @ShahidThaika That will be adding in a loop. Updating that – Suresh Atta Sep 01 '15 at 15:30
3

There is no concept of map literals in Java but you can easily write a utility method to achieve something similar. See this implementation for example. You can then initialise a map (using static imports):

Map<String, String> map = map("k1", "v1", "k2", "v2");

An alternative is to use the "double brace initialisation" syntax but it creates an anonymous inner class which is not necessarily a good idea.

assylias
  • 321,522
  • 82
  • 660
  • 783