Advertisement
Vasilena

ТЕМА_16_CW

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