Thursday, July 5, 2012

Iteration over a Map in Java


You can instead use the following code to iterate through each entry. A map is nothing but a series of Map.Entry objects. It is better to get a reference to Map.Entry and then iterate instead of getting all the keys and making the Map object do the work of fetching the value each time


for (Map.Entry<String, String> entry : map.entrySet())
{
    System.out.println(entry.getKey() + "/" + entry.getValue());
}
 

No comments:

Post a Comment