• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

guava multimap

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
5
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic