File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop through HashMap " Watch "Loop through HashMap " New topic

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

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

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
I agree. Here's the link:
subject: Loop through HashMap
It's not a secret anymore!