Multimap Interface
Multimap interface extends Map so that its keys can be mapped to multiple values at a time.
package org.fool.guava.collections;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class MultimapTest {
public static void main(String[] args) {
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");
Collection<String> lowerCollection = multimap.get("lower");
System.out.println("Initial lower case list");
System.out.println(lowerCollection.toString());
lowerCollection.add("f");
System.out.println("Modified lower case list");
System.out.println(lowerCollection.toString());
Collection<String> upperCollection = multimap.get("upper");
System.out.println("Initial upper case list");
System.out.println(upperCollection.toString());
upperCollection.remove("D");
System.out.println("Modified upper case list");
System.out.println(upperCollection.toString());
Map<String, Collection<String>> map = multimap.asMap();
System.out.println("Multimap as a map");
map.forEach((key, value) -> System.out.println(key + ":" + value));
System.out.println("Keys of Multimap");
Set<String> keys = multimap.keySet();
keys.forEach(key -> System.out.println(key));
System.out.println("Values of Multimap");
Collection<String> values = multimap.values();
System.out.println(values);
}
}
output
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 lower:[a, b, c, d, e, f] upper:[A, B, C] Keys of Multimap lower upper Values of Multimap [a, b, c, d, e, f, A, B, C]