//    File:  HT.java  -  Demonstrate use of a Hashtable

import java.util.Hashtable;
import java.util.Enumeration;

public class HT {

    public static void main(String[] args) {
        Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
        numbers.put("one",   new Integer(1));
        numbers.put("two",   2);    /* autoboxed */
        numbers.put("three", 3);    /* autoboxed */
        Integer n = (Integer) numbers.get("two");
        if (n != null)
            System.out.println("two = " + n);
        else
            System.out.println("NOPE");
        System.out.println("two = " + numbers.get("two") + "\n");    /* autoboxed */
        Hashtable<Integer, String> h = new Hashtable<Integer, String>();
        h.put (new Integer(1), new String("Mercury"));
        h.put (new Integer(2), new String("Venus"));
        h.put (new Integer(3), new String("Earth"));
        h.put (new Integer(4), new String("Mars"));
        h.put (new Integer(5), new String("Jupiter"));
        h.put (new Integer(6), new String("Saturn"));
        h.put (new Integer(7), new String("Uranus"));
        h.put (new Integer(8), new String("Neptune"));
        h.put (new Integer(9), new String("Pluto"));
        String s = h.get(new Integer(7));
        System.out.println("s = " + s);
        System.out.println("3 = " + h.get(3));
        Enumeration planets = h.keys();
        while (planets.hasMoreElements()) {
            Integer planetNo = (Integer) planets.nextElement();
            System.out.println(planetNo + " " + h.get(planetNo));
        }
        System.out.println();

        // create a new Hashtable with capacity=149, load factor=0.75f
        Hashtable<String, String> ht = new Hashtable<String, String>(149, 0.75f);
        // add some key-value pairs to the Hashtable
        ht.put("WA" , "Washington");
        ht.put("NY" , "New York");
        ht.put("RI" , "Rhode Island");
        ht.put("BC" , "British Columbia");

        // look up a key in the Hashtable
        String key = "NY";
        String stateName = (String) ht.get(key);
        System.out.println(stateName);    // prints "New York"

        // enumerate the contents of the hashtable
        Enumeration keys = ht.keys();
        while (keys.hasMoreElements()) {
            key = (String) keys.nextElement();
            stateName = (String) ht.get(key);
            System.out.println(key + " " + stateName);
        }
        System.out.println();
    }

}

/* ===== output =====
two = 2
two = 2

s = Uranus
3 = Earth
9 Pluto
8 Neptune
7 Uranus
6 Saturn
5 Jupiter
4 Mars
3 Earth
2 Venus
1 Mercury

New York
NY New York
RI Rhode Island
WA Washington
BC British Columbia

===== end ===== */
