Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

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

Written by Alfred

June 29, 2011 at 11:27

Posted in Java

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: