Multimap in Guava, Apache and Eclipse Collections


This multimap allows duplicate key-value pairs. JDK analogs are HashMap<K, List>, HashMap<K, Set> and so on.

Key's Order Value's Order Duplicate Analog Key Analog Value Guava Apache Eclipse (GS) Collections, JDK
Insertion-order Insertion-order Yes HashMap ArrayList ArrayListMultimap MultiValueMap FastListMultimap HashMap<K, ArrayList<V>>
not defined not defined Insertion-order LinkedHashMap ArrayList LinkedListMultimap MultiValueMap multiValueMap(new LinkedHashMap<K, List<>>(), ArrayList.class) LinkedHashMap<K, ArrayList<V>>
Insertion-order Insertion-order No LinkedHashMap LinkedHashSet LinkedHashMultimap MultiValueMap multiValueMap(new LinkedHashMap<K, Set<>>(), LinkedHashSet.class) LinkedHashMap<K, LinkedHashSet<V>>
sorted sorted No TreeMap TreeSet TreeMultimap MultiValueMap multiValueMap(new TreeMap<K, Set<>>(), TreeSet.class) TreeMap<K, TreeSet<V>>

Examples using Multimap

Task : Parse "Hello World! Hello All! Hi World!" string to separate words and print all indexes of every word using MultiMap (for example, Hello=[0, 2], World!=[1, 5] and so on)

1. MultiValueMap from Apache

String SAMPLE_TEXT = "Lorem Ipsum! Dolor Sit Amet. Lorem Ipsum! Consectetur Adipiscing Elit.";
 // Parse text to words and index
 List<String> terms = Arrays.asList(SAMPLE_TEXT.split(" "));
 // Create Multimap
 MultiMap<String, Integer> customMultiMap = new MultiValueMap<String, Integer>();
 // Fill Multimap
 int index = 0;
 for(String term: terms) {
 customMultiMap.put(term, index);
 index++;
 }
 // Print all terms
 System.out.println(customMultiMap); // print {Consectetur=[3], Amet.=[4], Sit=[2], Dolor=[1], Ipsum!=[0, 5],
                                       Elit.=[7], Lorem=[6]} - in random orders
 // Print all unique terms
 System.out.println(customMultiMap.keySet()); // print [Consectetur, Amet., Sit, Dolor, Ipsum!, Elit., Lorem] - in random orders
 // Print all indexes
 System.out.println("Lorem = " + customMultiMap.get("Lorem")); // print [6]
 System.out.println("Ipsum! = " + customMultiMap.get("Ipsum!")); // print [0, 5]
 System.out.println("Amet. = " + customMultiMap.get("Amet.")); // print [4]
 System.out.println("Elit. = " + customMultiMap.get("Elit.")); // print [7]
 System.out.println("Sit = " + customMultiMap.get("Sit")); // print [2]
 System.out.println("Dolor = " + customMultiMap.get("Dolor")); // print [1]
 System.out.println("Consectetur = " + customMultiMap.get("Consectetur")); // print [3]
 System.out.println("Nonexistent = " + customMultiMap.get("Nonexistent")); // print null
 // Print count unique terms
 System.out.println(customMultiMap.keySet().size()); // print 7

2. HashBiMap from GS / Eclipse Collection

import com.google.common.collect.*;
 
public class MultisetExample {
    public static void main(String[] args) {
        String[] germanWords = {"eins", "zwei", "drei", "Ball", "Schnee"};
        String[] frenchWords = {"un", "deux", "trois", "balle", "neige"};
 
        // Create Multiset
        MutableBiMap<String, String> biMap = new HashBiMap(germanWords.length);
 
        // Create German-French dictionary
        int i = 0;
        for (String germanWord : germanWords) {
            biMap.put(germanWord, frenchWords[i]);
            i++;
        }
 
        // Print count words
        System.out.println(biMap); // print {zwei=deux, Ball=balle, drei=trois, eins=un, Schnee=neige} - in random orders
 
        // Print all unique words
        System.out.println(biMap.keySet()); // print [Schnee, zwei, eins, drei, Ball] - in random orders
        System.out.println(biMap.values()); // print [deux, balle, un, neige, trois] - in random orders
 
        // Print translation by words
        System.out.println("eins = " + biMap.get("eins")); // print eins = un
        System.out.println("zwei = " + biMap.get("zwei")); // print zwei = deux
        System.out.println("Ball = " + biMap.inverse().get("Ball")); // print Ball = balle
        System.out.println("Schnee = " + biMap.inverse().get("Schnee")); // print Schnee = neige
        System.out.println("empty = " + biMap.get("empty")); // print empty = null
 
        // Print count word's pair
        System.out.println(biMap.size()); // print 5
    }
}

3. HashMultiMap from Guava

String customText = "Hello Universe! Hello Everyone! Hi Galaxy!";
List<String> tokens = Arrays.asList(customText.split(" "));
Multimap<String, Integer> indexMap = HashMultimap.create();
int counter = 0;
 
for (String token : tokens) {
    indexMap.put(token, counter);
    counter++;
}
 
System.out.println(indexMap); // print {Hi=[4], Hello=[0, 2], Universe!=[1, 5], Everyone!=[3]} - keys and values in random orders
System.out.println(indexMap.keySet()); // print [Hi, Hello, Universe!, Everyone!] - in random orders
System.out.println("Hello = " + indexMap.get("Hello")); // print [0, 2]
System.out.println("Universe = " + indexMap.get("Universe!")); // print [1, 5]
System.out.println("Everyone = " + indexMap.get("Everyone!")); // print [3]
System.out.println("Hi = " + indexMap.get("Hi")); // print [4]
System.out.println("Empty = " + indexMap.get("Empty")); // print []
System.out.println(indexMap.size()); // print 6
System.out.println(indexMap.keySet().size()); // print 4

Basic Programs