Search code examples
javaguava

Multimap with duplicated keys ordered by order with which they appear in the given multimap


I want to create multimap with keys as a Strings and values as a Sets

Multimap<String, Set<String>> = ???

when I put keys, and values in this way:

multimap.put("e", set1);
multimap.put("x", set2);
multimap.put("a", set3);
multimap.put("m", set4);
multimap.put("p", set5);
multimap.put("l", set6);
multimap.put("e", set7);

I want to receive exactly this same order with this same sets, so it means:

"e" -> set1
"x" -> set2
"a" -> set3
"m" -> set4
"p" -> set5
"l" -> set6
"e" -> set7

I'm new inn guava, so could anybody write how to implement this Multimap to set duplicated keys in it and receive values in this order?


Solution

  • LinkedHashMultimap.entries() preserves the exact order the entries were added to the multimap.