MapDemo.java.html

  1  import java.awt.Color;
  2  import java.util.HashMap;
  3  import java.util.Map;
  4  import java.util.Set;
  5  
  6  /**
  7     This program demonstrates a map that maps names to colors.
  8  */
  9  public class MapDemo
 10  {
 11     public static void main(String[] args)
 12     {      
 13        Map<String, Color> favoriteColors = new HashMap<String, Color>();
 14        favoriteColors.put("Juliet", Color.BLUE);
 15        favoriteColors.put("Romeo", Color.GREEN);
 16        favoriteColors.put("Adam", Color.RED);
 17        favoriteColors.put("Eve", Color.BLUE);
 18  
 19        // Print all keys and values in the map
 20  
 21        Set<String> keySet = favoriteColors.keySet();
 22        for (String key : keySet)
 23        {
 24           Color value = favoriteColors.get(key);
 25           System.out.println(key + " : " + value);
 26        }
 27     }
 28  }