In this article, we’ll explore the different possible ways of initializing an empty Map in Java.
We’ll be using Java 8 as well as Java 9 for checking out the different ways.
2. Using Java Collections
We can create an empty Map using the emptyMap() method provided by the Java Collections module. This will form an empty Map that is serializable in nature. The method was introduced in Java 1.5 under the Collections Library. This will create an immutable Map:
Map<String, String> emptyMap = Collections.emptyMap();
Note: Since the Map created is immutable in nature it will not allow the user to add any entries or perform any type of modifications to the Map. This will throw a java.lang.UnsupportedOperationException on trying to add or modify any key-value pair in the Map.
We have two more methods that support the creation and initialization of an empty Map. The emptySortedMap() returns an empty SortedMap of immutable type. A SortedMap is one that provides the further total ordering on its keys. The Map created by this method is serializable in nature:
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
The other method provided by Java Collections is emptyNavigableMap() which returns an empty NavigableMap. It has the same properties as that of an empty sorted Map. The only difference being this method returns a navigable Map. A Navigable Map is an extension of the traditional sorted Map implementation that returns the closest matches for a given search target.
NavigableMap<String, String> navigableMap = Collections.emptyNavigableMap();
All the above methods return Maps that are immutable in nature and we won’t be able to add any new entries to these Maps. This throws UnsupportedOperationException on forcefully trying to add, delete or modify any key-value pairs.
3. Initializing Map Using Constructors
We can initialize Maps using constructors of the different Map implementations i.e. HashMap, LinkedHashMap, TreeMap. All these initializations create an empty Map to which we can add entries later if required:
Map hashMap = new HashMap(); Map linkedHashMap = new LinkedHashMap(); Map treeMap = new TreeMap();
The above Maps are mutable and can accept new entries which is one of the advantages of using this approach. The Maps created during this type of initialization are empty. We can define empty Maps in a static block of code.
4. The Java 9 Way With Map.of()
Java 9 comes along with many new features such as Interface Private Methods, Anonymous classes, Platform Module System, and many more. The Map.of() is a factory method that was introduced in the Java 9 version. This method returns an immutable Map that creates zero mappings. The interface provided by this method comes under the Java Collections Framework. The Map.of(key1, value1, key2, value2, …..) can have at max 10 key-value pairs only.
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. An UnsupportedOperationException is thrown on trying to make any mutations in the Map after initialization. The. addition or the deletion of key-value pairs is also not supported and will result in throwing the above exception.
Note: The Map.of() method from Java 9 simplifies the initialization of immutable Maps with desired key-value pairs.
5. Using Guava
Until now we’ve looked into different ways of initializing an empty Map using core Java. Let’s move ahead now and check how to initialize a Map using the Guava library:
Map<String, String> articles = ImmutableMap.of();
The above method would create an immutable empty Map using the Guava Library.
In certain cases, we don’t need an immutable Map. We can initialize a mutable Map using the Maps class:
Map<String, String> emptyMap = Maps.newHashMap();
This type of initialization creates a mutable Map, i.e. we can add entries to this Map. But the basic initialization of this Map is empty and does not contain any entries.
We can also initialize the Map with specific key and value types. This will create a Map with predefined elements type and throw an exception if not followed:
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
In short, this creates an empty Map with key as string and value as an integer. The pair of angle brackets used for initialization is known as the Diamond Syntax. This will create a Map with the defined type arguments that invokes the constructor of the Maps class.
We can also create a mutable Map in guava using the below syntax:
Map<String, String> emptyMapUsingGuava = Maps.newHashMap(ImmutableMap.of());
In conclusion, the above method creates an empty Map in Java. We can add entries to this Map since it is mutable in nature.
The ImmutableMap.of() also overloaded method versions for creating Maps with entries. Since we are creating an empty Map, we don’t need to pass any parameters inside the method parenthesis to use the overloaded methods.
In this article, we have explored the different ways of initializing an Empty Map. We can see that there’s been a huge improvement in this field since Java 9. We have new factory methods for creating and initializing Maps.
As always, the complete source code is available over on Github.