How to eliminate duplicates in Guava MultiMap values?

user710818 picture user710818 · Sep 24, 2013 · Viewed 16.1k times · Source

Code:

    Multimap<String, String> myMultimap = ArrayListMultimap.create();
    myMultimap.put("12345", "qwer");
    myMultimap.put("12345", "abcd");
    myMultimap.put("12345", "qwer");
    System.out.println(myMultimap);

Result:

{12345=[qwer, abcd, qwer]}

Is it possible to eliminate duplicate "qwer" ? Thanks.

Answer

gustafc picture gustafc · Sep 24, 2013

Use one of the SetMultimap implementations, for example HashMultimap:

SetMultimap<String, String> myMultimap = HashMultimap.create();
myMultimap.put("12345", "qwer");
myMultimap.put("12345", "abcd");
myMultimap.put("12345", "qwer");
System.out.println(myMultimap); // {12345=[abcd, qwer]}