In this post, I’d like to share a tip about creating a Map object in Java with some fixed key-value pairs in just a single line. This tip will save you time in writing Java code, definitely.

You know, normally we create a Mapobject and put some initial pairs of key-value into it, as shown below:

Map<Integer, String> map = new HashMap<>();

map.put(200, "OK");
map.put(201, "Created");
map.put(301, "Moved Permanently");
map.put(403, "Forbidden");
map.put(404, "Not Found");
map.put(500, "Internal Server Error");

This is the trivial way to initialize a Map with some initial key-value pairs, using the Map’s put() method. However, this can be boilerplate and lengthy if there are more number of key-value pairs. There’s should be a better way, as described below.

 

1. Using Map.of() method

Since Java 9, you can use the following static method provided by the Map interface to initialize a Map with some fixed key-value pairs in just a single line:

Map.of(key1, value1, key2, value2, …)

This method creates and returns an immutable object of type Map, which holds the given key-value pairs. You can initialize up to 10 pairs of key-value pairs. So the above code example can be rewritten as follows:

Map<Integer, String> map = Map.of(
		200, "OK",
		201, "Created",
		301, "Moved Permanently",
		403, "Forbidden",
		404, "Not Found",
		500, "Internal Server Error"
	);

You see, this way makes the code more succinct and readable, right? Also it’s easier and more convenient to write.

 

2. Return a mutable Map object

Note that the Map.of() method returns an immutable object of type Map, which means you can’t put new key-value pairs or remove existing ones. Attempt to do so will result in UnsupportedOperationException thrown at runtime.

So if you want to have a modifiable Map object returned, you need to construct a new concrete Map type (e.g. HashMap) that wraps the object returned by the Map.of() method. The follow code example gives you the idea:

Map<Integer, String> map = new HashMap<>(Map.of(
		200, "OK", 
		201, "Created", 
		301, "Moved Permanently",
		403, "Forbidden", 
		404, "Not Found", 
		500, "Internal Server Error"
	));

Then you can put/remove key-value pairs to/from the map later, for example:

map.put(401, "Unauthorized");
map.remove(500);

 

Summary:

You have seen some code examples about how to initialize a Map object with some default key-value pairs in just one line, which is convenient and saves you time. Note that the Map.of() static method takes up to 10 pairs of key-value and it returns an immutable object of type Map. If you want to create a modifiable Map, you need to instantiate a new HashMap object that copies key-value pairs from the object created by Map.of() method.

I recommend you watch the following video to see the coding of Map initialization in action:

 

References:

 

Other Java Collections Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment