import java.lang.*; import java.util.Iterator; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Sample for Map. */ class MapSample { /** * Do it. */ public void doIt() { Map map = new HashMap(); // put into Map. map.put("red", "blood"); map.put("blue", "sky"); map.put("green", "grass"); // get values. String key = null; String value = null; key = "red"; value = (String)map.get(key); System.out.println("map.get(\"" + key + "\") = " + value); key = "blue"; value = (String)map.get(key); System.out.println("map.get(\"" + key + "\") = " + value); key = "green"; value = (String)map.get(key); System.out.println("map.get(\"" + key + "\") = " + value); // Map to Iterator. Set set = map.keySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { String key1 = (String)iterator.next(); String value1 = (String)map.get(key1); System.out.println(key1 + " -> " + value1); } } public static void main(String args[]) { MapSample sample = new MapSample(); sample.doIt(); } }