This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to Iterate over a hashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to Iterate over a hashMap" Watch "How to Iterate over a hashMap" New topic
Author

How to Iterate over a hashMap

Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi,
I am trying to iterate over two the hashMaps where I need the combination of elements from both the hashMaps. The iteration must be something similar to bubble Sort

Instead of using an ArrayList I need to use a HashMap which holds the value and its associated count.I am unable to figure out what to use in place of indexes(i and j)
Please help me
Thanks in advance
Pavel Kubal
Ranch Hand

Joined: Mar 13, 2004
Posts: 356
Why don't you itereate over keys and through keys you can get values?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Why are you using legacy code like Vector and StringTokenizer?
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Campbell Ritchie wrote:Why are you using legacy code like Vector and StringTokenizer?


Would you please explain it in detail.
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Pavel Kubal wrote:Why don't you itereate over keys and through keys you can get values?


Hey it would be great if you could help me with some sample code.
Please translate the above code and help me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

If you need the key-value pairs, then don't iterate over the keys and get the values. It's more efficient to iterate over the entries of the map, so that you don't have to do a lookup in the map for each key. Each entry is a key-value pair. For example:

About Campbell's remark: Class Vector is a legacy collection class. It has been replaced by ArrayList since Java 1.2. There's no good reason anymore to use the old class Vector. Instead of StringTokenizer, you can use the newer and more convenient class Scanner.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22


Here instead of vector Candidates I have to use a hashmap and iterate similarly.
Please help me how to iterate over this loop with hashmap.
Just help me convert this code as required.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

read java.util.HashMap . and Jesper explained well to your query already
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Jesper Young wrote:

map does not implement Iterable so you can't use it directly in a for-each loop. You surely mean map.entrySet()


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Yes Rob, my mistake, it should have been:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Thank you, Jesper, for answering my question.
 
 
subject: How to Iterate over a hashMap