aspose file tools*
The moose likes Beginning Java and the fly likes same key for  diff values in HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "same key for  diff values in HashMap" Watch "same key for  diff values in HashMap" New topic
Author

same key for diff values in HashMap

Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You could use a List to hold the values, and assign "fruit" to that List.


[My Blog]
All roads lead to JavaRanch
Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
[Not-so-nice response removed. Antagonizing people who are trying to help you is not a winning strategy.]
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

What (I think) Christopher meant was this...




[List of FAQs] | [Android FAQ] | [Samuh Varta]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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?




Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Dec 21, 2006
Posts: 197
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.
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
Change the HashMap construtor to this:

Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Dec 21, 2006
Posts: 197
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Dec 21, 2006
Posts: 197
Expected output is::

fruit: mango
fruit apple
fruit: orange
fruit: banana
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Jan 17, 2006
Posts: 1296
Is that just a single "\n" separated String? That's easy then

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: same key for diff values in HashMap