HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How to directly initialize a HashMap (in a literal way)?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
literalhowinitializehashmapwaydirectly

Problem

Is there some way of initializing a Java HashMap like this?:

Map test = 
    new HashMap{"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.

Solution

All Versions

In case you happen to need just a single entry: There is Collections.singletonMap("key", "value").
For Java Version 9 or higher:

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 test1 = Map.of(
    "a", "b",
    "c", "d"
);

// this works for any number of elements:
import static java.util.Map.entry;    
Map 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"));. Also note that in this case keys and values must not be null.

(See also JEP 269 and the Javadoc)
For up to Java Version 8:

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 myMap = new HashMap() {{
    put("a", "b");
    put("c", "d");
}};


However, the anonymous subclass might introduce unwanted behavior in some cases. This includes for example:

  • It generates an additional class which increases memory consumption, disk space consumption and startup-time



  • In case of a non-static method: It holds a reference to the object the creating method was called upon. That means the object of the outer class cannot be garbage collected while the created map object is still referenced, thus blocking additional memory



Using a function for initialization will also enable you to generate a map in an initializer, but avoids nasty side-effects:

Map myMap = createMap();

private static Map createMap() {
    Map myMap = new HashMap();
    myMap.put("a", "b");
    myMap.put("c", "d");
    return myMap;
}

Code Snippets

// 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")
);
Map<String, String> myMap = new HashMap<String, String>() {{
    put("a", "b");
    put("c", "d");
}};
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;
}

Context

Stack Overflow Q#6802483, score: 2252

Revisions (0)

No revisions yet.