Java HashMap Inline Initialization

Following examples demonstrate how to initialize a Java HashMap using standard and inline methods.

Why do we need to directly initialize a HashMap?

Although Maps are widely used to collect and work with data in a dynamic way, often you will need to create a small map with predefined keys and values just to test a short algorithm or a concept you are working on. Another use-case to initialize a Java HashMap by hand is to test how your program or algorithm performs with specific values. While writing short demonstration programs you will most probably prefer to initialize the map directly instead of reading the data from a file, or from some kind of stream and fill the map with values.  You can imagine how much more effort you will need to do this compared to a single one-line HashMap initialization.

Java initialize HashMap

The Imperative Way of Initializing a HashMap

The straight forward solution is to declare a Map and then just put() entries into the Map. We will use this example as reference to compare with other techniques that are more type saving.

Map<String,String> mymap = new HashMap<String, String>();

As you can see in the example above, you first create a map object and put entries to it. Another important thing to mention here is that by using this methods you create a dynamic Map and you are able to edit, remove or create new entries.

Use Anonymous Subclass to Initialize a HashMap

This creates an anonymous subclass of HashMap, whose instance initializer puts these values. In other words it creates a new class that inherits from HashMap. This is the shortest way to create and initialize a dynamic HashMap

Map<Integer, String> mymap = new HashMap<Integer, String>() {
		put(1, "one");
		put(2, "two");

Initialize Immutable Maps

Immutable maps, compared to dynamic maps (shown in the examples above), are non editable. This means you can not put new entries into it, remove entries or update them. In case you want to test a simple algorithm they will just work fine for you.

Initialize Immutable Map in Java SE 8 and Earlier Versions

Following example demonstrates how to initialize immutable Map in Java 8 or earlier Java versions:

Map<Integer,String> mymap = new HashMap<>();
Map<Integer,String> immutableMap = Collections.unmodifiableMap(mymap);

As you can see, you create and initialize the Map the old-fashioned way. What makes the map immutable is the call to Collections.unmodifiableMap(Map map). Well it, doesn’t look much like an inline initialization. No worries, next example does.

Inline Initialize a HashMap the Guava way

Guava was the first one to introduce true one-line Map initialization. See the example below

Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);

Inline Initialize a HashMap the Java 9 Way

And finally in Java 9 we can use one-line statements to initialize immutable HashMaps without the need of 3rd party libraries.

Map<Integer,String> map = Map.of(1, "A", 2, "B", 3, "C");

You may want to read the complete Java 9 Immutable Map Example for more details.


Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply

Be the First to Comment!