aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics and Colletions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics and Colletions" Watch "Generics and Colletions" New topic
Author

Generics and Colletions

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661


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

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

Can someone please explain.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Apr 29, 2008
Posts: 47
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics and Colletions