28

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.

gustafc
  • 28,465
  • 7
  • 73
  • 99
user710818
  • 23,228
  • 58
  • 149
  • 207

3 Answers3

48

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]}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
gustafc
  • 28,465
  • 7
  • 73
  • 99
  • 5
    As the docs state, a reference to a plan Multimap is seldom preferred over one of the subinterfaces for the same reasons that references to Collection are rarely preferred over Set, List, etc. The LHS of the assignment should be a SetMultimap. – gk5885 Sep 24 '13 at 16:48
14

A ListMultimap such as ArrayListMultimap allows duplicate key-value pairs. Try an implementation of SetMultimap such as HashMultimap or TreeMultimap.

Paul Blessing
  • 3,815
  • 2
  • 24
  • 25
  • 2
    @StormeHawke I don't have to blink when I read it, but it sure hurts looking at. Thing is, when you come back a year later to maintain this code, you have to figure out just how this particular wheel was reinvented. Will it contain empty sets for keys with no values? If so, are there empty values for *all* expected keys, or just for some? Will there be nulls among the values? And then you start cursing the original author ("Damn you, past me!") for not using a collection with a clearly defined contract and intent, i.e. multimap. – gustafc Sep 24 '13 at 15:21
1

There are many ways to do this. The simplest would be to use a SetMultimap.

A JDK only solution with your given example however would be to simply use a Map<String, Set<String>>, which would have one unique key to a Set of unique values.

Map<String, Set<String>> map = new HashMap<String, Set<String>>();

The advantage of using that is you don't have to bring in data structures from outside libraries, you're strictly using the java core libraries.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • 1
    Happy to see plain Java solution. – Silviu Burcea Sep 24 '13 at 14:01
  • @SilviuBurcea `MultiMap`s have their place, but the need for them is pretty rare in my experience... I've never needed to use one in all the time I've been programming – StormeHawke Sep 24 '13 at 14:03
  • 2
    I guess all the google groupies don't like my pure java solution... only reason I can see for down votes on a perfectly valid answer – StormeHawke Sep 24 '13 at 16:48
  • I downvoted your answer because it is, at best, incomplete. Your brief code sample creates a Map, but doesn't populate it. Thus, the conclusion that it is either "better" or "more intuitive" is unsupported. – gk5885 Sep 24 '13 at 16:55
  • 2
    @gk5885 why does my example need to populate the map? He clearly already knows how to populate a map just from reading his question. This points him in the right direction and gives him plenty to run with. – StormeHawke Sep 24 '13 at 16:58
  • 1
    The OP's example demonstrates that he/she knows how to populate a `Multimap`, not a `Map>`, which does, in fact, require somewhat more complexity. – Louis Wasserman Sep 24 '13 at 17:00
  • 1
    There's no difference in population methods, and if for some strange reason the OP wasn't familiar, he could very easily get examples via everybody's good friend, google – StormeHawke Sep 24 '13 at 17:53