File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HashMap key/value pairs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap key/value pairs" Watch "HashMap key/value pairs" New topic
Author

HashMap key/value pairs

laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Is it possible for a HashMap to use a Object as a key and the value be an ArrayList?



If it's valid, how would you obtain the Object information while iterating through the HashMap?

Would something like this work? Keeping in mind that the Object holds information like name, phone, address and the ArrayList would be a list of Contact Objects? I tried something like below but it didn't work.


If this is not a good idea, do you have a suggestion on what could be done differently?

Thank You.
Arek Sokolowski
Greenhorn

Joined: Jan 12, 2011
Posts: 8

Hi, your key is an Object, and value - ArrayLists - contains also Objects. Object does not have 'name' or 'contactname' properties (getters)
Try to declare HashMap like this:
Such code declares a HashMap, where keys are 'Person' class objects, and values are ArrayList object, and each element in this list is also a Person class object.

I'm not sure if it will work with JSP, but it is ok for plain java.
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Thank you for the tip. It's working now.
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

laura mccord wrote:

I tried something like below but it didn't work.





to access the contents of ArrayList, You would need a loop inside a loop, right?
laura mccord
Ranch Hand

Joined: Feb 10, 2009
Posts: 108
Yes you're right. You have to do an embedded loop on the value (the key) to get the values to the other object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap key/value pairs