This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 8 in Action this week in the Java 8 forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop through HashMap
 
Similar Threads
HashMap
Using Properties instead of HashMap when dealing with Strings
Iteration speed of Collections
List in HashMap
Looping through HashMap