This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization 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