Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loop through HashMap

 
Joe Man
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone show me how to loop through a HashMap?
I have a HashMap that contains 20 keys and 20 values. I need to loop through all of those keys and values and display it all. may be using a for loop..
Thanks in advance..
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
You need to do something like this:

You'll have to typecast the Object back to its true class like this:

Hope this helps
Michael Morris
SCJP
 
Joe Man
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot!!
I really appreciate your help..
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to know keys as well as values, then the values() method will be of limited use. Most efficient would be to use entrySet() instead, followed by getKey() and getValue().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic