Multimap in Guava Apache and Eclipse Collections

suggest change

A multimap maps each key to more than one value. Some of the implementations below allow duplicate key-value pairs, and some do not. JDK analogs are HashMap<K, List> and HashMap<K, Set>.

Key's order Value's order Duplicate Analog key Analog value Guava Apache Eclipse (GS) Collections JDK
not defined Insertion-order yes HashMap ArrayList ArrayListMultimap MultiValueMap FastListMultimap HashMap<K, ArrayList<V>>
not defined not defined no HashMap HashSet HashMultimap MultiValueMap.multiValueMap(new HashMap<K, Set>(), HashSet.class); UnifiedSetMultimap HashMap<K, HashSet<V>>
not defined sorted no HashMap TreeSet Multimaps.newMultimap(HashMap, Supplier<TreeSet>) MultiValueMap.multiValueMap(new HashMap<K, Set>(), TreeSet.class) TreeSortedSetMultimap HashMap<K, TreeSet<V>>
Insertion-order Insertion-order yes LinkedHashMap ArrayList LinkedListMultimap MultiValueMap.multiValueMap(new LinkedHashMap<K, List>(), ArrayList.class); LinkedHashMap<K, ArrayList>
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 the string “Hello World! Hello All! Hi World!” into words and print every index of each word using a multimap (for example, Hello=[0, 2], World!=[1, 5], and so on).

1. MultiValueMap from Apache

String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Parse text to words and index
List<String> words = Arrays.asList(INPUT_TEXT.split(" "));
// Create Multimap
MultiMap<String, Integer> multiMap = new MultiValueMap<String, Integer>();
// Fill Multimap
int i = 0;
for(String word: words) {
    multiMap.put(word, i);
    i++;
}

// Print all words
System.out.println(multiMap); // print {Hi=[4], Hello=[0, 2], World!=[1, 5], All!=[3]} - in random order
// Print all unique words
System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random order

// Print all indexes
System.out.println("Hello = " + multiMap.get("Hello"));    // print [0, 2]
System.out.println("World = " + multiMap.get("World!"));    // print [1, 5]
System.out.println("All = " + multiMap.get("All!"));    // print [3]
System.out.println("Hi = " + multiMap.get("Hi"));    // print [4]
System.out.println("Empty = " + multiMap.get("Empty"));    // print null

// Print count unique words
System.out.println(multiMap.keySet().size());    //print 4

2. HashBiMap from GS / Eclipse Collections

String[] englishWords = {"one", "two", "three","ball","snow"};
String[] polishWords = {"jeden", "dwa", "trzy", "kula", "snieg"};

// Create a BiMap
MutableBiMap<String, String> biMap = new HashBiMap(englishWords.length);
// Create an English-Polish dictionary
int i = 0;
for(String englishWord: englishWords) {
    biMap.put(englishWord, polishWords[i]);
    i++;
}

// Print the dictionary
System.out.println(biMap); // print {two=dwa, ball=kula, one=jeden, snow=snieg, three=trzy} - in random order
// Print all unique words
System.out.println(biMap.keySet());    // print [snow, two, one, three, ball] - in random order
System.out.println(biMap.values());    // print [dwa, kula, jeden, snieg, trzy] - in random order

// Print translations
System.out.println("one = " + biMap.get("one"));    // print one = jeden
System.out.println("two = " + biMap.get("two"));    // print two = dwa
System.out.println("kula = " + biMap.inverse().get("kula"));    // print kula = ball
System.out.println("snieg = " + biMap.inverse().get("snieg"));    // print snieg = snow
System.out.println("empty = " + biMap.get("empty"));    // print empty = null

// Print the number of pairs
System.out.println(biMap.size());    //print 5

3. HashMultimap from Guava

String INPUT_TEXT = "Hello World! Hello All! Hi World!";
// Parse text to words and index
List<String> words = Arrays.asList(INPUT_TEXT.split(" "));
// Create Multimap
Multimap<String, Integer> multiMap = HashMultimap.create();

// Fill Multimap
int i = 0;
for(String word: words) {
    multiMap.put(word, i);
    i++;
}

// Print all words
System.out.println(multiMap); // print {Hi=[4], Hello=[0, 2], World!=[1, 5], All!=[3]} - key and value order is not defined
// Print all unique words
System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random order

// Print all indexes
System.out.println("Hello = " + multiMap.get("Hello"));    // set containing 0 and 2; order is not defined
System.out.println("World = " + multiMap.get("World!"));    // set containing 1 and 5; order is not defined
System.out.println("All = " + multiMap.get("All!"));    // set containing 3
System.out.println("Hi = " + multiMap.get("Hi"));    // set containing 4
System.out.println("Empty = " + multiMap.get("Empty"));    // empty collection, not null

// Print count all words
System.out.println(multiMap.size());    //print 6

// Print count unique words
System.out.println(multiMap.keySet().size());    //print 4

More examples:

I. Apache Collection:

  1. MultiValueMap
  2. MultiValueMapLinked
  3. MultiValueMapTree

II. GS / Eclipse Collection

  1. FastListMultimap
  2. HashBiMap
  3. TreeSortedSetMultimap
  4. UnifiedSetMultimap

III. Guava

  1. HashMultiMap
  2. LinkedHashMultimap
  3. LinkedListMultimap
  4. TreeMultimap
  5. ArrayListMultimap

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents