GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Loop through HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop through HashMap " Watch "Loop through HashMap " New topic
Author

Loop through HashMap

Joe Man
Ranch Hand

Joined: Feb 17, 2002
Posts: 71
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..


Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Joe Man
Ranch Hand

Joined: Feb 17, 2002
Posts: 71
Thanks alot!!
I really appreciate your help..
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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().


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loop through HashMap