Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Posts Tagged ‘hashmap

Java | Loop a Map

leave a comment »

I think this is the best way I found to loop through a Map in Java.

public void printMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println("Key:" + pairs.getKey() + ", Value:" + pairs.getValue());
    }
}

Reference Source

Advertisements

Written by Alfred

June 29, 2011 at 11:27

Posted in Java

Tagged with , ,