Advertisement
Boyan5

Title 16 exercise in class

Mar 22nd, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import java.util.Dictionary;
  2. import java.util.Enumeration;
  3. import java.util.Hashtable;
  4.  
  5. public class dictionary {
  6.     public static void main(String[] args) {
  7.         Dictionary<Integer,String> MyDictionary = new Hashtable<>(16);
  8.         //              key : value
  9.         MyDictionary.put(1,"Apple");
  10.         MyDictionary.put(2,"Banana");
  11.         MyDictionary.put(3,"Orange");
  12.         MyDictionary.put(4,"Avocado");
  13.  
  14.         // PRINT MyDictionary BEFORE removing elements
  15.         System.out.println();
  16.         System.out.println("BEFORE removing elements: " + MyDictionary.size());
  17.         System.out.println();
  18.  
  19.         // print SIZE() of dictionary BEFORE REMOVING ELEMENTS ..................................
  20.         System.out.println("\nSize of my_dict BEFORE removing elements : " + MyDictionary.size());
  21.         System.out.println();
  22.  
  23.  
  24.         // using GET() method, when the key is INTEGER ...........................................
  25.         System.out.println("Value at key = 1 : " + MyDictionary.get(1));
  26.         System.out.println("Value at key = 2 : " + MyDictionary.get(2));
  27.         System.out.println("Value at key = 3 : " + MyDictionary.get(3));
  28.         System.out.println("Value at key = 4 : " + MyDictionary.get(4));
  29.         System.out.println();
  30.  
  31.         // using GET() method, when the key is STRING.............................................
  32.         /*
  33.         System.out.println("Value at key = AP : " + MyDictionary.get("AP"));
  34.         System.out.println("Value at key = BA : " + MyDictionary.get("BA"));
  35.         System.out.println("Value at key = OR : " + MyDictionary.get("OR"));
  36.         System.out.println("Value at key = AV : " + MyDictionary.get("AV"));
  37.         System.out.println();
  38. */
  39.  
  40.  
  41.  
  42.         // PRINT BY FOR-CYCLE ITERATION WHEN KEY IS INTEGER
  43.         System.out.println("By FOR-CYCLE :");
  44.         for (int i = 1; i <= MyDictionary.size(); i++) {
  45.  
  46.             System.out.println("MyDict element : " + MyDictionary.get(i));
  47.  
  48.         }
  49.  
  50.         System.out.println();
  51.  
  52.  
  53.  
  54.         // FOR-EACH-CYCLE ::::::::::::::::: HASH-TABLE
  55.         // PRINT KEYS BY ENUMERATOR USING KEYS() METHOD...................................................
  56.         System.out.println("By Enumeration - KEYS :");
  57.         for (Enumeration items = MyDictionary.keys(); items.hasMoreElements();)
  58.         {
  59.             System.out.println("KEYS in Dictionary : " + items.nextElement());
  60.             //System.out.println(items.asIterator());
  61.         }
  62.         System.out.println();
  63.  
  64.         // FOR-EACH-CYCLE ::::::::::::::::: HASH-TABLE
  65.         // PRINT VALUES (ELEMENTS) BY ENUMERATOR USING ELEMENTS() METHOD....................................................
  66.         System.out.println("By Enumeration - VALUES :");
  67.         for (Enumeration items = MyDictionary.elements(); items.hasMoreElements();)
  68.         {
  69.             System.out.println("VALUES in Dictionary : " + items.nextElement());
  70.         }
  71.         System.out.println();
  72.  
  73.         // using isEmpty() method..............................................................
  74.         System.out.println("\nIs my dictionary empty? : " + MyDictionary.isEmpty());
  75.         System.out.println();
  76.  
  77.  
  78.         // using REMOVE() method...............................................................
  79.         // remove value by Integer key 1 and key 2
  80.         MyDictionary.remove(1);
  81.         MyDictionary.remove(2);
  82.         System.out.println();
  83.  
  84.         // remove value by String key-"BA" and key-"AV"
  85.         MyDictionary.remove("BA");
  86.         MyDictionary.remove("AV");
  87.         System.out.println();
  88.  
  89.  
  90.         // CHECK  BY INTEGER-KEY IF THE ELEMENTS ARE REMOVED
  91.         System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(1));
  92.         System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(2));
  93.         System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(3));
  94.         System.out.println();
  95.  
  96.         // CHECK  BY STRING-KEY IF THE ELEMENTS ARE REMOVED
  97.         System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("BA"));
  98.         System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("AV"));
  99.         System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("AP"));
  100.         System.out.println();
  101.  
  102.  
  103.  
  104.         // print after removing elements
  105.         System.out.println("AFTER removing elements : " + MyDictionary);
  106.         System.out.println();
  107.  
  108.  
  109.         // using size() method.................................................................
  110.         System.out.println("\nSize of my_dict AFTER removing elements : " + MyDictionary.size());
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement