Posted By

vonkinder on 05/05/08


Tagged

iteration map iterator


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

zdroshnya


Iterate Map - Dump Map


 / Published in: Java
 

  1. Iterator it = mp.entrySet().iterator();
  2. while (it.hasNext()) {
  3. Map.Entry pairs = (Map.Entry)it.next();
  4. System.out.println(pairs.getKey() + " = " + pairs.getValue());

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: vonkinder on May 21, 2008

To iterate any Map(map, hashMap, etc).

Posted By: the_coder on June 4, 2008

Or, with Generics (replace "Key" and "Value" with the actual types of the key and value) in Java 1.5+:

Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); }

Or, combined with the for-each loop in Java 1.5+:

Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); }

Posted By: the_coder on June 4, 2008

try again [code] Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); } [/code]

[code] Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); } [/code]

Posted By: the_coder on June 4, 2008

Map<Key,Value> mp;

Iterator<Map.Entry<Key,Value>> it = mp.entrySet().iterator();

while (it.hasNext()) {

Map.Entry&lt;Key,Value&gt; pair = it.next();

System.out.println(pair.getKey() + " = " + pair.getValue());

}

Map<Key,Value> mp;

for (Map.Entry<Key,Value> pair : mp.entrySet()) {

System.out.println(pair.getKey() + " = " + pair.getValue());

}

Posted By: the_coder on June 4, 2008

Okay, got it. Or, with Generics (replace "Key" and "Value" with the actual types of the key and value) in Java 1.5+: Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); }

Or, combined with the for-each loop in Java 1.5+: Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); }

Posted By: the_coder on June 4, 2008

Map<Key,Value> mp;

Iterator<Map.Entry<Key,Value>> it = mp.entrySet().iterator();

while (it.hasNext()) {

    Map.Entry<Key,Value> pair = it.next();

    System.out.println(pair.getKey() + " = " + pair.getValue());

}

Map<Key,Value> mp;

for (Map.Entry<Key,Value> pair : mp.entrySet()) {

    System.out.println(pair.getKey() + " = " + pair.getValue());

}

You need to login to post a comment.