document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Tugas : Simple Hash Tabble
  3.  *
  4.  * Rafael Asi Kristanto Tambunan
  5.  * 5025201168
  6.  * Teknik Informatika
  7.  */
  8.  
  9. import java.util.Hashtable;
  10. import java.util.Iterator;
  11.  
  12. public class HashTable
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.         //1. Create Hashtable
  17.         Hashtable<Integer, String> hashtable = new Hashtable<>();
  18.          
  19.         //2. Add mappings to hashtable
  20.         hashtable.put(1,  "A");
  21.         hashtable.put(2,  "B" );
  22.         hashtable.put(3,  "C");
  23.          
  24.         System.out.println(hashtable);
  25.          
  26.         //3. Get a mapping by key
  27.         String value = hashtable.get(1);        //A
  28.         System.out.println(value);
  29.          
  30.         //4. Remove a mapping
  31.         hashtable.remove(3);            //3 is deleted
  32.          
  33.         //5. Iterate over mappings
  34.         Iterator<Integer> itr = hashtable.keySet().iterator();
  35.          
  36.         while(itr.hasNext())
  37.         {
  38.             Integer key = itr.next();
  39.             String mappedValue = hashtable.get(key);
  40.              
  41.             System.out.println("Key: " + key + ", Value: " + mappedValue);
  42.         }
  43.     }
  44. }
');