ListMultimap. This collection, part of com.google.common.collect, is a generic map to lists. Unlike a HashMap, we can store many values at one key.
With this class, we call put() and get() just like in a HashMap. But on multiple calls to get() with the same key, the values are all stored, not replaced. Get() returns a list.
First example. We call ArrayListMultimap.create to begin. We use the generic syntax in Java—we specify String keys and Integer (int) values.
Detail This adds a key-value pair to the ListMultimap. We can call put on the same key more than once with no error.
Detail This retrieves a list of all the values located at this key. We can loop over this with for.
Detail This tells us whether a key exists or not in the map. It returns true for "dog" in this program.
import java.util.List;
import com.google.common.collect.*;
public class Program {
public static void main(String[] args) {
// Create a map to ArrayList values.ListMultimap<String, Integer> map = ArrayListMultimap.create();
// Add pairs to ListMultimap.
map.put("cat", 5);
map.put("dog", 10);
map.put("cat", 20);
// Get list at this key.// ... Iterate over its values.
List<Integer> list = map.get("cat");
for (int value : list) {
System.out.println(value);
}
// See if ListMultimap contains this key.
if (map.containsKey("dog")) {
System.out.println(true);
}
}
}5
20
true
Remove. This method receives two arguments: a key and a value. It removes only one value in the list at a key. So the key may still have values within the ListMultimap.
Here We add three values to the key "sky." We then remove one, the value 20. The other two values still exist at the key.
import com.google.common.collect.*;
public class Program {
public static void main(String[] args) {
// Create a ListMultimap and add data to it.ListMultimap<String, Integer> map = ArrayListMultimap.create();
map.put("lawn", 10);
map.put("sky", 50); // Add three values at sky.
map.put("sky", 20);
map.put("sky", 100);
// Remove one of the values at sky.
map.remove("sky", 20);
// Sky now has just two values.
System.out.println(map);
}
}{sky=[50, 100], lawn=[10]}
Nonexistent key. What happens when we call get() but no values exist at that key? In this example we find an empty list is returned. It has zero elements, but is not null.
Tip To see if a key exists, please use containsKey. This makes code clearer and easier to understand.
import java.util.List;
import com.google.common.collect.*;
public class Program {
public static void main(String[] args) {
// Create an empty ListMultimap.ListMultimap<String, Integer> map = ArrayListMultimap.create();
// It is empty.
System.out.println(map.isEmpty());
// Remove one of the values at sky.
List<Integer> list = map.get("Hyperion");
// A nonexistent value list is empty, not null.
System.out.println(list);
System.out.println(list.isEmpty());
System.out.println(list.size());
}
}true
[]
true
0
With ListMultimap, we have a normal map collection. But at each key, many values may exist. We can add these values just with the key and put(). No special logic is required.
As with other Guava classes, this collection is a solution for a common problem. I find multimaps are less often used than things like HashMap. But this need is not rare.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 22, 2022 (edit).