File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to iterate a HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to iterate a HashMap" Watch "How to iterate a HashMap" New topic
Author

How to iterate a HashMap

Jan-Henrik Clausen
Greenhorn

Joined: Jun 28, 2010
Posts: 13
I seem to have a brain block - after some hours of trying, reading, googling I haven't find the solution yet for a problem which is surely quite simple.

I want to iterate through a HashMap. The relevant code in my class is like follows:



What is the type of "something"? In the API I read "Map.Entry<K,V>" as a result for it.Next() (the next pair of key=value in my HashMap Tix). But how do I apply that to my case?


----

Sorry, hours of fruitless brain wrecking preceded this post but minutes after posting I found the solution myself. The following code seems to work:




Sorry again for bothering you - I did not find an option to erase the thread.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

You take the Map.Entry object which is assigned to the "something" variable and you do... something... with it. You don't say what you want to do or why you want to do anything at all, so it's pretty hard to go beyond that. The Map.Entry object contains a HashMap key and its corresponding value; it has methods to extract those things; from there it's up to you to use them for whatever you need to use them for.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Claudius:

Why not just get the key Set and iterate over that?

John.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is commonly called the "rubber duck effect" -- the act of explaining something, even to an inanimate object like a rubber duck, is very often enough to give you the insight to solve it yourself.


[Jess in Action][AskingGoodQuestions]
Jan-Henrik Clausen
Greenhorn

Joined: Jun 28, 2010
Posts: 13
John de Michele wrote:Claudius:

Why not just get the key Set and iterate over that?

John.


My aim was to read out the HashMap and store it in a textfile ("key=value" lines). I omitted the corresponding code because my question was aimed solely at the kind of type "something" requires. I could iterate over the key Set and extract the corresponding value from the Hashmap during the iteration. But is this faster or more readable than iterating over the complete entries? I do not see a decisive advantage in that but that does not mean anything. I "see" at the moment as a total newbie. If I have overlooked something important please teach me.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Claudius Calvus wrote:My aim was to read out the HashMap and store it in a textfile ("key=value" lines). I omitted the corresponding code because my question was aimed solely at the kind of type "something" requires. I could iterate over the key Set and extract the corresponding value from the Hashmap during the iteration. But is this faster or more readable than iterating over the complete entries? I do not see a decisive advantage in that but that does not mean anything. I "see" at the moment as a total newbie. If I have overlooked something important please teach me.


What you describe is precisely the sort of thing I would do using the key set:

You'll notice that I use the new-style, or foreach loop. This obviates the need to deal with iterators directly.

John.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Here's the equivalent using a modern for loop and entrySet():

The advantage of using entrySet() is that it ultimately requires less work from the CPU than using keySet() followed by map.get(). Specifically the map.get(key) call is extra work that shouldn't be necessary - the entrySet() gives you all key-value pairs directly, without having to repeat the lookup for each key. This effect is more noticeable if you use a slower Map like TreeMap - for HashMap it often won't matter. Still, it can't hurt to get used to iterating a map using entrySet(), as it's a good practice in general.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Ernest Friedman-Hill wrote:This is commonly called the "rubber duck effect" -- the act of explaining something, even to an inanimate object like a rubber duck, is very often enough to give you the insight to solve it yourself.

Yes, I noticed that very early in my career. Only it was usually my boss I was explaining the problem to, not a rubber duck. And yes, there was a difference. No "boss" jokes please. I have never had a bad boss.
Jan-Henrik Clausen
Greenhorn

Joined: Jun 28, 2010
Posts: 13
Mike Simmons wrote:Here's the equivalent using a modern for loop and entrySet():

The advantage of using entrySet() is that it ultimately requires less work from the CPU than using keySet() followed by map.get(). Specifically the map.get(key) call is extra work that shouldn't be necessary - the entrySet() gives you all key-value pairs directly, without having to repeat the lookup for each key. This effect is more noticeable if you use a slower Map like TreeMap - for HashMap it often won't matter. Still, it can't hurt to get used to iterating a map using entrySet(), as it's a good practice in general.


Thanks, that was very helpful. Being a newbie and coming from Delphi this kind of for-loop was new for me (I did not even think that such a loop could exist in a programming language...). That shortens the code quite nicely.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to iterate a HashMap