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 | ––––– | ———–– | –––––| ———–| –––––|—––| —––| ———————––|—–| 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) | TreeSortedSet- Multimap | 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 “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 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 orders
// Print all unique words
System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random orders

// 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 Collection

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

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

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

// Print translate by words
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 count word's pair
System.out.println(biMap.size());    //print 5
  1. 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]} - keys and values in random orders
// Print all unique words
System.out.println(multiMap.keySet());    // print [Hi, Hello, World!, All!] - in random orders

// 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 []

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

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

Nore examples:

I. Apache Collection:

  1. MultiValueMap
  2. MultiValueMapLinked
  3. MultiValueMapTree

II. GS / Eclipse Collection

  1. FastListMultimap
  2. HashBagMultimap
  3. TreeSortedSetMultimap