• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Generics and Colletions

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic