Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.Hashtable;
  2.  
  3. public class HashtableExample
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //1. Create Hashtable
  8.         Hashtable<Integer, String> hashtable = new Hashtable<>();
  9.  
  10.         //2. Add mappings to hashtable
  11.         hashtable.put(1,  "A");
  12.         hashtable.put(2,  "B" );
  13.         hashtable.put(3,  "C");
  14.  
  15.         System.out.println(hashtable);
  16.  
  17.         //3. Get a mapping by key
  18.         String value = hashtable.get(1);        //A
  19.         System.out.println(value);
  20.  
  21.         //4. Remove a mapping
  22.         hashtable.remove(3);            //3 is deleted
  23.  
  24.         //5. Iterate over mappings
  25.  
  26.         for (Integer key : hashtable.keySet()) {
  27.             String mappedValue = hashtable.get(key);
  28.  
  29.             System.out.println("Key: " + key + ", Value: " + mappedValue);
  30.         }
  31.     }
  32. }