Literal declaration of HashMap in Java
Solution 1
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.
Solution 2
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.
Shahid Thaika
I am an IT professional with over 10 years of work experience
Updated on June 04, 2022Comments
-
Shahid Thaika about 2 years
In
JavaScript
, you can declare all the keys and value combinations for aJSON
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
forHashMaps
.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?
-
Maroun almost 9 yearsYes.. I don't like the double "{" solution.
-
assylias almost 9 years@JordiCastilla or not! The map will hold a reference to the enclosing class which may be undesirable...
-
Jordi Castilla almost 9 yearsjust missing a quote in
"Value 3
.... ;) -
Jordi Castilla almost 9 years@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...
-
Shahid Thaika almost 9 yearsI 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.
-
Suresh Atta almost 9 years@ShahidThaika you can freely choose your answer :)
-
Shahid Thaika almost 9 yearsI 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.
-
Suresh Atta almost 9 years@ShahidThaika My answer will be different if you ask for
what is the best way to add elements
, My answer purely concentrated onLiteral declaration of HashMap in Java
:) -
Shahid Thaika almost 9 yearsCan you add that too, then I will close by choosing this reply.
-
Suresh Atta almost 9 years@ShahidThaika That will be adding in a loop. Updating that