• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to iterate a HashMap

 
Jan-Henrik Clausen
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20168
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudius:

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

John.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan-Henrik Clausen
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20168
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic