• 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

same key for diff values in HashMap

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I want to assign same key but different values in hashmap, Map key is unique right

Ex:

like this I want to add, how can I achieve this in hashmap.

regards,
vardhan
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a List to hold the values, and assign "fruit" to that List.
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Not-so-nice response removed. Antagonizing people who are trying to help you is not a winning strategy.]
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What (I think) Christopher meant was this...



 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vardhan reddy wrote:Hi All,

I want to assign same key but different values in hashmap, Map key is unique right

Ex:

like this I want to add, how can I achieve this in hashmap.

regards,
vardhan



if this were possible, what should be printed here?



 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
monu thanks for your reply..

hashmap is not taking list as argument, it is giving error: change fruitsList to String

Garrett

the out put is banana only it will overried .

regards,
vardhan
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hashmap is not taking list as argument, it is giving error


What kind of error do you have ? Please post the exact error message.
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chris,

error is::The method put(String, String) in the type HashMap<String,String> is not applicable for the arguments
(String, List<String>)

we are passing list as argument here.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the HashMap construtor to this:

 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jay,

this the error::

The method put(String, String) in the type HashMap<String,String> is not applicable for the arguments
(String, List<String>)

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vardhan reddy wrote:jay,

this the error::

The method put(String, String) in the type HashMap<String,String> is not applicable for the arguments
(String, List<String>)



You're not even trying Monu's code, are you ?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vardhan reddy wrote:
Garrett

the out put is banana only it will overried .

regards,
vardhan



I know what the output is, what do you want the ouptut to be?
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris

I am checking with monu's code only in that one only I am getting this error.

Garrett

Expected out put is for same kay I want different values as i mention in my first post.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That doesn't make sense. If you call hashmap.get("fruit"), and you have multiple values for the key "fruit", do you want it to return an array, a List, a comma separated String? How do you want to receive these different values?
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expected output is::

fruit: mango
fruit apple
fruit: orange
fruit: banana
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not here to give you any straight answers, but think a few minutes about it :
1. The problem with a map is that you can assign only one value per key
2. To assign several values per key, we suggested you to use a Map<String, List><String>>. The values are stored in a List, and the List is assigned to the key of the map

Try to find out how :
1. To iterate through the Map. What is the type of each value ?
=> Hint: It's not different from any other Map
2. To iterate through each value.
=> Hint: How do you iterate through a List
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that just a single "\n" separated String? That's easy then

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's quite nasty Garret. What if you want to remove one of the values, but not the rest? Or iterate over all the values for one key?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:That's quite nasty Garret.


I thought if I put the very dirty version out there it might motivate the OP to clarify his requirements.

What if you want to remove one of the values, but not the rest? Or iterate over all the values for one key?


I'll leave contains() and remove() as an exercise for the reader.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic