Guava – Multimap Interface

  • Post author:
  • Post category:Guava
  • Post comments:1 Comment
Multimap interface

Multimap interface extends Map so that its keys can be mapped to multiple values at a time.

Interface Declaration

Following is the declaration for com.google.common.collect.Multimap<K,V> interface −

@GwtCompatible
public interface Multimap<K,V>

Interface Methods

Sr.NoMethod & Description
1Map<K,Collection<V>> asMap()Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key’s associated values.
2void clear()Removes all key-value pairs from the multimap, leaving it empty.
3boolean containsEntry(Object key, Object value)Returns true if this multimap contains at least one key-value pair with the key and the value.
4boolean containsKey(Object key)Returns true if this multimap contains at least one key-value pair with the key.
5boolean containsValue(Object value)Returns true if this multimap contains at least one key-value pair with the value.
6Collection<Map.Entry<K,V>> entries()Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.
7boolean equals(Object obj)Compares the specified object with this multimap for equality.
8Collection<V> get(K key)Returns a view collection of the values associated with key in this multimap, if any.
9int hashCode()Returns the hash code for this multimap.
10boolean isEmpty()Returns true if this multimap contains no key-value pairs.
11Multiset<K> keys()Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.
12Set<K> keySet()Returns a view collection of all distinct keys contained in this multimap.
13boolean put(K key, V value)Stores a key-value pair in this multimap.
14boolean putAll(K key, Iterable<? extends V> values)Stores a key-value pair in this multimap for each of values, all using the same key, key.
15boolean putAll(Multimap<? extends K,? extends V> multimap)Stores all key-value pairs of multimap in this multimap, in the order returned by multimap.entries().
16boolean remove(Object key, Object value)Removes a single key-value pair with the key and the value from this multimap, if such exists.
17Collection<V> removeAll(Object key)Removes all values associated with the key.
18Collection<V> replaceValues(K key, Iterable<? extends V> values)Stores a collection of values with the same key, replacing any existing values for that key.
19int size()Returns the number of key-value pairs in this multimap.
20Collection<V> values()Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()).

Example of Multimap

Create the following java program using any editor of your choice in say C:/> Guava.

GuavaTester.java

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class GuavaTester {
   public static void main(String args[]) {
   
      GuavaTester tester = new GuavaTester();
      Multimap<String,String> multimap = tester.getMultimap();

      List<String> lowerList = (List<String>)multimap.get("lower");
      System.out.println("Initial lower case list");
      System.out.println(lowerList.toString());

      lowerList.add("f");
      System.out.println("Modified lower case list");
      System.out.println(lowerList.toString());

      List<String> upperList = (List<String>)multimap.get("upper");
      System.out.println("Initial upper case list");
      System.out.println(upperList.toString());

      upperList.remove("D");
      System.out.println("Modified upper case list");
      System.out.println(upperList.toString());

      Map<String, Collection<String>> map = multimap.asMap();
      System.out.println("Multimap as a map");

      for (Map.Entry<String,  Collection<String>> entry : map.entrySet()) {
         String key = entry.getKey();
         Collection<String> value =  multimap.get("lower");
         System.out.println(key + ":" + value);
      }

      System.out.println("Keys of Multimap");
      Set<String> keys =  multimap.keySet();

      for(String key:keys) {
         System.out.println(key);
      }

      System.out.println("Values of Multimap");
      Collection<String> values = multimap.values();
      System.out.println(values);
   }

   private Multimap<String,String> getMultimap() {

      //Map<String, List<String>>
      // lower -> a, b, c, d, e
      // upper -> A, B, C, D

      Multimap<String,String> multimap = ArrayListMultimap.create();

      multimap.put("lower", "a");
      multimap.put("lower", "b");
      multimap.put("lower", "c");
      multimap.put("lower", "d");
      multimap.put("lower", "e");

      multimap.put("upper", "A");
      multimap.put("upper", "B");
      multimap.put("upper", "C");
      multimap.put("upper", "D");		

      return multimap;
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester
See the result.

Initial lower case list
[a, b, c, d, e]
Modified lower case list
[a, b, c, d, e, f]
Initial upper case list
[A, B, C, D]
Modified upper case list
[A, B, C]
Multimap as a map
upper:[a, b, c, d, e, f]
lower:[a, b, c, d, e, f]
Keys of Multimap
upper
lower
Values of Multimap
[a, b, c, d, e, f, A, B, C]

Previous Page:-Click Here

This Post Has One Comment

Leave a Reply