• Post Reply Bookmark Topic Watch Topic
  • New Topic

guava multimap  RSS feed

 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a school project that requires the use of a hashmap and I'm done and ready to turn it in.  It takes keys and values and it works fine.  The only issue I have is that if someone enters duplicate keys, in this case a name, it will only print the last duplicate entered.  So what I want to do is use the multimap feature that comes with guava.

So here is my question, how can I read my hashmap into my new multimap?  I have to take my hashmap data into a mutlmap where it will keep the duplicates, then print out the multimap for my final report.  In the real world I could use multimap directly, but that isn't an option here.  I'm probably pushing it to use mutlimap in this project anyway. 

Thanks
 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Houser wrote:In the real world I could use multimap directly, but that isn't an option here.

If you have a plain map (from Java SE framework), a key is only associated to one value. Not more. However nothing stops you from having a kind of collection as the value.

Say for example a HashMap<String,List<Integer>>

Converting this map to a Guava multimap is rather easy.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you have a plain map (from Java SE framework), a key is only associated to one value. Not more. However nothing stops you from having a kind of collection as the value.

Say for example a HashMap<String,List<Integer>>

Ok, so I could have two values that correspond to one key by doing what you show above.  Got it, thanks.
I'm still unsure on the next step,
Converting this map to a Guava multimap is rather easy.

How about one more hint? 

Thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials and find the sections about Maps. I think there is an example with a Map whose “V”s are Lists.
 
Alex Houser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials and find the sections about Maps. I think there is an example with a Map whose “V”s are Lists.


There is.  Looking at their multimap section now where they use lists.  Thanks.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!