Advertisement
Denis_Hristov

Dictionary_ClassWork

Mar 16th, 2021 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.Dictionary;
  2. import java.util.Enumeration;
  3. import java.util.Hashtable;
  4.  
  5. public class Rechnik {
  6.     public static void main(String[] args) {
  7.         Dictionary<Integer, String> MyDictionary = new Hashtable<>();
  8.  
  9.         MyDictionary.put(1, "Apple");
  10.         MyDictionary.put(2, "Bannana");
  11.         MyDictionary.put(3, "Orange");
  12.         MyDictionary.put(4, "Avocado");
  13.  
  14.         /*
  15.         MyDictionary.put(AP, "Apple");
  16.         MyDictionary.put(BA, "Bannana");
  17.         MyDictionary.put(OR, "Orange");
  18.         MyDictionary.put(AV, "Avocado");
  19.         */
  20.  
  21.  
  22.         System.out.println("\nSize of my dict: " + MyDictionary.size());
  23.  
  24.         System.out.println("Value at key = 1: " + MyDictionary.get(1));
  25.         System.out.println("Value at key = 2: " + MyDictionary.get(2));
  26.         System.out.println("Value at key = 3: " + MyDictionary.get(3));
  27.         System.out.println("Value at key = 4: " + MyDictionary.get(4));
  28.  
  29.         System.out.println("Before removing elements" + MyDictionary);
  30.  
  31.         System.out.println("By for-cycle: ");
  32.         for (int i = 1; i <= MyDictionary.size(); i++) {
  33.             System.out.println("MyDict element: " + MyDictionary.get(i));
  34.         }
  35.  
  36.         System.out.println("By enumeration-keys: ");
  37.         for (Enumeration items = MyDictionary.keys(); items.hasMoreElements();) {
  38.             System.out.println("Key in Dictionary: " + items.nextElement());
  39.         }
  40.  
  41.         System.out.println("By enumeration-values: ");
  42.         for (Enumeration items = MyDictionary.elements(); items.hasMoreElements();) {
  43.             System.out.println("Value in Dictionary: " + items.nextElement());
  44.         }
  45.  
  46.         System.out.println("\nIs my dictionary empty? : " + MyDictionary.isEmpty() + "\n");
  47.  
  48.         MyDictionary.remove(1);
  49.         MyDictionary.remove(2);
  50.  
  51.         MyDictionary.remove("BA");
  52.         MyDictionary.remove("AV");
  53.  
  54.         System.out.println("Cheking if the removed value exists(By Integer key): " + MyDictionary.get(1));
  55.         System.out.println("Cheking if the removed value exists(By Integer key): " + MyDictionary.get(2));
  56.  
  57.         System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("BA"));
  58.         System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("AV"));
  59.         System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("AP"));
  60.  
  61.  
  62.         System.out.println("After removing elements: " + MyDictionary);
  63.  
  64.         System.out.println("\nSize of my dict: " + MyDictionary.size());
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement