Guava ArrayListMultimap put() and putAll() methods in Java
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 theArrayListMultimapand always returnstrue. The method adds the value to the end of the list of values associated with the key. The method does not throw aNullPointerExceptionif either the key or the value isnull.boolean putAll(K key, Iterable<? extends V> values):This method stores a key-value pair in theArrayListMultimapfor each value in the specified iterable, all using the same key. The method returnstrueif theArrayListMultimapchanged 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 aNullPointerExceptionif the iterable isnull.boolean putAll(Multimap<? extends K, ? extends V> multimap):This method stores all key-value pairs of multimap in thisArrayListMultimap, in the order returned bymultimap.entries(). The method returnstrueif theArrayListMultimapchanged 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 aNullPointerExceptionif multimap isnull.
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.ArrayListMultimap; class Main { public static void main(String[] args) { // Create an empty multimap ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create(); // Store a key-value pair in the multimap multimap.put("a", 1); System.out.println(multimap); // prints {a=[1]} // Store another key-value pair with the same key in the multimap multimap.put("a", 2); System.out.println(multimap); // prints {a=[1, 2]} } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; import java.util.Arrays; class Main { public static void main(String[] args) { // Create an empty multimap ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create(); // Store multiple values for a key in the multimap multimap.putAll("a", Arrays.asList(3, 4, 5)); System.out.println(multimap); // prints {a=[3, 4, 5]} multimap.put("a", 5); // Store multiple values for another key in the multimap multimap.putAll("b", ImmutableList.of(6, 7)); multimap.putAll("b", ImmutableList.of(7, 8)); System.out.println(multimap); // {a=[3, 4, 5, 5], b=[6, 7, 7, 8]} } } |
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()orputAll()method and get the result. - They are consistent and predictable. The
put()andputAll()methods preserve the insertion order of the elements in the multimap. They also throw aNullPointerExceptionif iterable or multimap isnull. - 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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)