The Google Guava libraries provide a rich set of utilities for creating mutable and immutable Maps. This article will discuss various methods to initialize a map using Guava in Java.

1. Mutable Map

Maps.newHashMap() creates a mutable HashMap instance with the same mappings as the specified map. We should use this method if you need to add or remove entries later.

 
If no argument is passed to Maps.newHashMap(), an empty HashMap instance is created.

 
This method is deprecated in Java 7 and above.

2. Immutable Maps

Guava provides several simple, easy-to-use immutable implementations of Map. Guava’s immutable list does not allow any null key or value (why?). It will throw a NullPointerException if any key or value in the map is null and an UnsupportedOperationException if any modification operation is performed on it. Since the returned map is immutable, any change in the underlying mutable map will not be visible in the unmodifiable map.

ImmutableMap.copyOf() method

ImmutableMap.copyOf returns an immutable map containing the same entries as the specified map. Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. If the map somehow contains entries with duplicate keys (for example, if it is a SortedMap whose comparator is not consistent with equals), the results of this method are undefined.

ImmutableMap.Builder

Guava also provides a builder for creating immutable map instance using Builder’s putAll() method. putAll() associates all the given map’s keys and values in the built map.

ImmutableMap.of() method

Guava’s ImmutableMap.of() returns an immutable map containing the given entries in order. It will throw an IllegalArgumentException if duplicate keys are provided.

 
For example,

 
The above code will create a map {United States=Washington D.C., United Kingdom=London}

 
This method supports up to 5 key/value pairs. To create a map with more than 5 entries, we can use Builder’s put() method, which takes key-value pairs instead of a map. put() associates key with value in the built map. Duplicate keys are not allowed and will cause build() to fail.

3. Immutable Sorted Maps

A SortedMap provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys or by a provided Comparator.

Guava’s ImmutableSortedMap throws:

  1. NullPointerException if any key or value in the map is null.
  2. UnsupportedOperationException if any modify operation is performed on it (Any change in the underlying mutable map will not be visible in the unmodifiable map).
  3. IllegalArgumentException if any two keys are equal according to their natural ordering or provided comparator.

ImmutableSortedMap.copyOf() method

It has many variations:

1. Returns an immutable map containing the same entries as the provided sorted map, with the same ordering.

 
2. Returns an immutable map containing the same entries as the map, sorted by the natural ordering of the keys.

 
3. Returns an immutable map containing the same entries as the map, with keys sorted by the provided comparator.

ImmutableSortedMap.Builder

Guava also provides a builder for creating immutable sorted map instances whose keys are ordered by the provided comparator.

ImmutableSortedMap.of() method

Guava’s ImmutableSortedMap.of() returns an immutable sorted map containing the given entries, sorted by the natural ordering of their keys. Its prototype is similar to ImmutableMap.of()

4. Using Multimap

Multimap is a collection that maps keys to values, similar to a map, but each key may be associated with multiple values. We can use Multimap to associate each key with multiple values. There are many implementations of Multimap. The most commonly used ones are ArrayListMultimap that internally uses an ArrayList, and HashMultimap uses hash tables to store the values for a given key.

Download Code

That’s all about initializing Map in Java using Guava Library.

 
Related Posts:

Initialize Map in Java

Initialize Map in Java using Apache Commons Collections