There are only two ways to traverse a map:
1. Traverse through the keyset of the map
2. Traverse through the entryset of the map
[Java] View plaincopy on code to see a snippet derived from my Code slice
Map Traversal method One: Traverse through the map's keyset
@Test
public void Test4 () {
Map<integer, string> map = new Hashmap<integer, string> ();
Map.put (1, "good");
Map.put (2, "Morning");
set<integer> set = Map.keyset ();
for (Integer Ky:set) {
SYSTEM.OUT.PRINTLN (ky + ":" + Map.get (ky));
}
System.out.println ("-------------------");
}
Map Traversal method Two: Traverse through Map's EntrySet
@Test
public void Test5 () {
Map<integer, string> map = new Hashmap<integer, string> ();
Map.put (1, "good");
Map.put (2, "Morning");
Set<map.entry<integer, string>> set = Map.entryset ();
For (Entry<integer, string> entry:set) {
System.out.println (Entry.getkey () + ":" + entry.getvalue ());
}
System.out.println ("-------------------");
}
Map Traversal method