Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and Colletions

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is from MAster Exam.
I cant understand the for statement.

for(Map.Entry<K,V> entry:in.entrySet())

Can someone please explain.
 
Henry Wong
author
Marshal
Pie
Posts: 21510
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from MAster Exam.
I cant understand the for statement.

for(Map.Entry<K,V> entry:in.entrySet())

Can someone please explain.


It would help a bit if you elaborate on what parts you don't understand. Is it how the variable "in" is defined with generics? Is it what the entrySet() method does/return? Or is it the for (each) loop?

Henry
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


It would help a bit if you elaborate on what parts you don't understand. Is it how the variable "in" is defined with generics? Is it what the entrySet() method does/return? Or is it the for (each) loop?

Henry



for(Map.Entry<K,V> entry:in.entrySet())
out.put(entry.getValue(),entry.getKey());


I didnot understand what is Map.Entry<K,V> entry
what is entry and Map.Entry here?

in is the name of the map referencing map m1 which is retriveing the keys and values via entrySet() - If I am thinking right...
 
Henry Wong
author
Marshal
Pie
Posts: 21510
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnot understand what is Map.Entry<K,V> entry
what is entry and Map.Entry here?


Entry is a static inner (nested) class of the Map interface. It is basically, a data class that has a field to hold the key and a field to hold the value. It is used here because the loop needs to have both the key and value.

Henry
 
Seema Gaurav
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila,
Correct me if I'm wrong, I'm in the learning mode here.
To add to what Henry said, that statement helps you iterate through the Map.
Unlike Sets and Lists, Maps don't come with methods that return iterators. To workaround this, the Map API gives you the method entrySet() that returns a Set to facilitate iteration. Every element in this set is an object that implements the static interface Map.Entry.

Consider how the for each loop would have been, if you had a set of Objects in them:


Now since entrySet() returns a Set containing a set of Map.Entry elements, we can say-


Here's the sample code to sum it up:


You can visit the Map.Entry API for more details. Hope this is clear. Somebody correct me, if I'm wrong. I'm quite new to Generics and Collections.


HTH,
Seema
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic