/**
* @author lautturi.com
* Java example: iterate through a hashmap in java
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
Map map = new HashMap();
map.put(1,"Apple");
map.put(2,"Banana");
map.put(3,"Cherry");
map.put(4,"Orange");
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
String value = (String)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
}
}
output:
Key = 1, Value = Apple Key = 2, Value = Banana Key = 3, Value = Cherry Key = 4, Value = Orange