• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap key/value pairs

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the tip. It's working now.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you're right. You have to do an embedded loop on the value (the key) to get the values to the other object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic