• Post Reply Bookmark Topic Watch Topic
  • New Topic

for (Map.Entry<String, String> entry : browsers.entrySet())  RSS feed

 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have a question can anyone explain what this piece of code exactly does?


browser is a Map with both Strings as K,V-pairs.

Does this put every K,V-pair from the browser-Map one by one into the entry-Map?
And why use Map.Entry? What does this do?

Thanks in advance,
Kenneth
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a Map, you can iterate over all the keys (using the keySet() method) or all the values (using the values() method). But if you need to use both the key and value it can be more efficient to iterate over the key-value pairs. That's what entrySet() does - it returns a set of key-value pairs. Since that has to be a set of single objects, it uses Map.Entry to encapsulate each pair. If you look at the docs you'll see Map.Entry has got methods getKey() and getValue().
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, i did a little more research.

Is it right to say that :

Makes a map entry where K,V-pairs as a whole can be written into?
And

returns a K,V-pair as a whole?

So this is the way to itterate throug a map's entrys without actually having to know what the key's are?
Or am I seeing it wrong?

Thanks,
Kenneth
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or to put it another way, this:
is roughly equivalent to
but it's more efficient because it doesn't have to separately look up the value for each key.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:So this is the way to itterate throug a map's entrys without actually having to know what the key's are?
Or am I seeing it wrong?

Sounds like you're on the right track. Basically, a Map contains "mappings", which are single pairs of a key and its associated value. Usually you'll deal with them one at a time with get() and put(), but sometimes you may want to process ALL the data in the Map, and that's what entrySet() does for you: it hands you all the data in the Map as a Set of Map.Entry's.

Furthermore, if the Map (eg, a TreeMap) has any defined order, then the entries will be in that order; and regardless, unless the Map is updated between invocations, they will always be in the same order.

HIH

Winston
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the answers.
I understand it now, somethimes the same explanation in different words
makes everything much easier to understand.

Thanks again,
Kenneth
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!