Displaying map content on top of an iterator

I am trying to display map

which I created using an Iterator. The code I'm using:

private void displayMap(Map<String, MyGroup> dg) {
Iterator it = dg.entrySet().iterator();   //line 1
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove();
   }
}

      

MyGroup class, and it has two fields: id

and name

. I want to display these two values ​​versus pair.getValue()

. The problem here is that Line 1 never gets executed and doesn't throw any exceptions.

Please, help.

PS: I tried every method on this link .

+3


source to share


1 answer


Map<String, MyGroup> map = new HashMap<String, MyGroup>();  
for (Map.Entry<String, MyGroup> entry : map.entrySet()) {
     System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

      

using an iterator



Map<String, MyGroup> map = new HashMap<String, MyGroup>();
Iterator<Map.Entry<String, MyGroup>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<String, MyGroup> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

      

For more details on iteration see this link

+7


source







All Articles