Is there some way of initializing a Java HashMap like this?:
Map<String,String> test = new HashMap<String, String>{"test":"test","test":"test"};
What would be the correct syntax? I have not found anything regarding this. Is this possible? I am looking for the shortest/fastest way to put some "final/static" values in a map that never change and are known in advance when creating the Map.
This is one way. Map<String, String> h = new HashMap<String, String>() {{ put("a","b"); }};
The Static Initializer for a Static HashMap We can also initialize the map using the double-brace syntax: Map<String, String> doubleBraceMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};
For initializing an empty Map, we'll not pass any key-value pair in this method: Map<String, String> emptyMapUsingJava9 = Map. of(); This factory method produces an immutable Map, hence we'll not be able to add, delete or modify any key-value pair.
In case you happen to need just a single entry: There is Collections.singletonMap("key", "value")
.
Yes, this is possible now. In Java 9 a couple of factory methods have been added that simplify the creation of maps :
// this works for up to 10 elements: Map<String, String> test1 = Map.of( "a", "b", "c", "d" ); // this works for any number of elements: import static java.util.Map.entry; Map<String, String> test2 = Map.ofEntries( entry("a", "b"), entry("c", "d") );
In the example above both test
and test2
will be the same, just with different ways of expressing the Map. The Map.of
method is defined for up to ten elements in the map, while the Map.ofEntries
method will have no such limit.
Note that in this case the resulting map will be an immutable map. If you want the map to be mutable, you could copy it again, e.g. using mutableMap = new HashMap<>(Map.of("a", "b"));
(See also JEP 269 and the Javadoc)
No, you will have to add all the elements manually. You can use an initializer in an anonymous subclass to make the syntax a little bit shorter:
Map<String, String> myMap = new HashMap<String, String>() {{ put("a", "b"); put("c", "d"); }};
However, the anonymous subclass might introduce unwanted behavior in some cases. This includes for example:
Using a function for initialization will also enable you to generate a map in an initializer, but avoids nasty side-effects:
Map<String, String> myMap = createMap(); private static Map<String, String> createMap() { Map<String,String> myMap = new HashMap<String,String>(); myMap.put("a", "b"); myMap.put("c", "d"); return myMap; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With