Initialize Map using Guava Library in Java

The Google Guava libraries provides a rich set of utilities for creating mutable and immutable Maps. In this article, we will discuss various methods to initialize map using Guava in Java.

 

1. Mutable Map:

Maps.newHashMap() creates a mutable HashMap instance with same mappings as the specified map. This method should be used 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?). A NullPointerException will be thrown if any key or value in map is null and an UnsupportedOperationException if any modify operation is performed on it. Since returned map is immutable, any change in the underlying mutable map will not be visible in the unmodifiable map.

 

2.1. ImmutableMap.copyOf()

ImmutableMap.copyOf returns an immutable map containing the same entries as specified map. Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. If 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.

 

2.2. ImmutableMap Builder

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

 

2.3. ImmutableMap.of()

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,

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 Sorted Map 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 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.

 

3.1. ImmutableSortedMap.copyOf()

ImmutableSortedMap.copyOf() 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 map, sorted by the natural ordering of the keys.

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

 

3.2. ImmutableSortedMap Builder

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

 

3.3. ImmutableSortedMap.of()

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. Multimap

Multimap is a collection that maps keys to values, similar to Map, but in which 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, most commonly used ones are ArrayListMultimap that internally uses an ArrayList and HashMultimap that uses hash tables to store the values for a given key.

 

 
Related Posts:

1. Initialize Map in Java

2. Initialize Map using Apache Commons Collections in Java

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz