In this post, we will explore how to use the put() and putAll() methods of the ArrayListMultimap class, which are used to add key-value pairs to the multimap.

One of the classes that Guava offers is ArrayListMultimap, which uses a HashMap to associate each key with an ArrayList of values. It is an implementation of the ListMultimap interface, which extends the Multimap interface. A ListMultimap is a multimap that stores the values for each key in a list. This means that the order of values for a given key is preserved, duplicate values are allowed, and it supports random access of values by index. The ArrayListMultimap class has several methods to manipulate the key-value pairs in the multimap, such as get, remove, removeAll, replaceValues, and more. In this post, we will focus on two of these methods: put() and putAll().

1. Overview of put() and putAll() methods

The put() and putAll() methods are instance methods that store key-value pairs in the multimap. The put() method takes a single key and a single value as parameters, and adds them to the multimap. The putAll() method takes a single key and an iterable of values as parameters, and adds them all to the multimap. Both methods return a boolean value indicating whether the multimap changed as a result of the operation. The put() and putAll() methods have the following signatures:

  • boolean put(K key, V value): This method stores a key-value pair in the ArrayListMultimap and always returns true. The method adds the value to the end of the list of values associated with the key. The method does not throw a NullPointerException if either the key or the value is null.
  • boolean putAll(K key, Iterable<? extends V> values): This method stores a key-value pair in the ArrayListMultimap for each value in the specified iterable, all using the same key. The method returns true if the ArrayListMultimap changed as a result. The method adds the values to the end of the list of values associated with the key, in the order they appear in the iterable. The method throws a NullPointerException if the iterable is null.
  • boolean putAll(Multimap<? extends K, ? extends V> multimap): This method stores all key-value pairs of multimap in this ArrayListMultimap, in the order returned by multimap.entries(). The method returns true if the ArrayListMultimap changed as a result. The method adds the values to the end of the list of values associated with each key, preserving their order within multimap. The method throws a NullPointerException if multimap is null.

2. Usage of put() method

To use the put() and putAll() methods, we need to have an instance of ArrayListMultimap first. We can create an empty multimap using the create() method. Once we have an ArrayListMultimap instance, we can call the put() method on it to store a single key-value pair in the multimap. Here is an example of how to use the put() method to associate a key with a value in a ArrayListMultimap collection.

Download Code

3. Usage of putAll() method

We can also call the putAll() method on it to store multiple values for the same key in the multimap. We can use any iterable of values as a parameter for the putAll() method, such as a list, a set, or another collection. Here is an example of how to use the putAll() method add multiple values for a key in a ArrayListMultimap collection.

Download Code

 
Notice that the order of the values is preserved by their insertion order, and that duplicate values are allowed for each key.

4. Benefits of using the put() and putAll() methods

The put() and putAll() methods have several advantages over other ways of creating or modifying multimaps. Some of them are:

  • They are concise and expressive. We just need to pass the key and value(s) as parameters to the put() or putAll() method and get the result.
  • They are consistent and predictable. The put() and putAll() methods preserve the insertion order of the elements in the multimap. They also throw a NullPointerException if iterable or multimap is null.
  • It allows duplicate key-value pairs, which can be useful for some applications.

That’s all about put() and putAll() methods of the ArrayListMultimap class in Guava. If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.