Thursday, November 25, 2010

HashMap Iteration

// ArrayList
public static void dumpList(ArrayList al)
{
Iterator itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}

}

//Hash Set
public static void Set(HashSet hs)
{
Iterator itr = hs.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}

}


// HashMap
public static void dumpMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}

No comments:

Post a Comment