-4

I am very sorry for this basic thing could any one please explain about how this for will work.

   for (Map.Entry<Object, Object> en : m.entrySet()) {
            Object object = en.getKey();
            Object object1 = en.getValue();

        } 
Barry Chapman
  • 6,690
  • 3
  • 36
  • 64
user3214269
  • 219
  • 2
  • 8
  • 23

4 Answers4

0

That is a so called for each loop you simply iterate over all values in that map.

The class Map.Entry just holds the combination of key and value so you can handle both informations at once.

rekire
  • 47,260
  • 30
  • 167
  • 264
0

For each (key, value) pair in m.entrySet(), set "object" to the key and "object1" to the value

Bauhaus
  • 509
  • 8
  • 22
0

This for loop obtains one entry of the map, stores it in en and then executes the body of the loop. The body of the loop stores the key, and value in object and object1 respectively. Then, the next iteration commences, wherein the next entry in the map is obtained. This process is continued till every item in the map has been iterated over/processed. This loop is also called a for each (for each item in some group of objects!) loop and is often used to iterate over collections.

batbrat
  • 5,155
  • 3
  • 32
  • 38
0

"m" is an instance of the Map, so, m.entrySet() returns the entire set of the <key,value> entries in the map. Now the loop iterates from 0 to the number of elements in "m"(i.e, m.size()), each entry is stored into "en" and the loop is iterated, i.e, the "key" of the current element is stored into "object" and "value" for the same into "object1".

Hope it helped.. :)

deb_rider
  • 570
  • 2
  • 12