aspose file tools*
The moose likes Java in General and the fly likes Map having same keys ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map having same keys ..." Watch "Map having same keys ..." New topic
Author

Map having same keys ...

Anand Jooshi
Greenhorn

Joined: Jun 15, 2009
Posts: 8
Hi,


Can somebody tell me how retrive the values from any map having same keys ??


Thanks ,
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
Anand Jooshi wrote:Can somebody tell me how retrive the values from any map having same keys ??


Maps can't contain duplicate keys


Joanne
Anand Jooshi
Greenhorn

Joined: Jun 15, 2009
Posts: 8

...does it mean that if I put 3 values inside a map having same key then by any means I will not get all these values back .

I will get only last value which I put ?



Thanks,
Theodore Casser
Ranch Hand

Joined: Mar 14, 2001
Posts: 1902

Yes, that's correct - you'll only get back the last value for that key.


Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
If you want to associate multiple values with a single key in a map, then you need to put those values in to a List and then associate that list with the key.
When you want to add a value to the map, you first check if the key already exists. If it does you retrieve the List and add the new value to it. If it doesn't then you create a List, add your new value to it and then put that key/List pair into the Map.
Anand Jooshi
Greenhorn

Joined: Jun 15, 2009
Posts: 8
Thanks everybody !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

You can check out Apache Commons Collections, it has a MultiMap interface with two implementing classes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Map having same keys ...