Advertisement
MilaDimitrovaa

Class

Mar 16th, 2021
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Dictionary;
  4. import java.util.Enumeration;
  5. import java.util.Hashtable;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.      Dictionary <String , String> MyDictionary = new Hashtable();
  12.  
  13.  
  14.        // Използване PUT метода
  15.         //               key : value
  16.  
  17.        /* MyDictionary.put(1,"Apple");
  18.         MyDictionary.put(2,"Banana");
  19.         MyDictionary.put(3,"Orange");
  20.         MyDictionary.put(4,"Avocado"); */
  21.  
  22.             MyDictionary.put("AP","Apple");
  23.             MyDictionary.put("BA","Banana");
  24.             MyDictionary.put("OR","Orange");
  25.             MyDictionary.put("AV","Avocado");
  26.  
  27.         // принтираме SIZE() на речника - преди да премахнем елементите
  28.  
  29.         System.out.println("\nSize of my_dict : " + MyDictionary.size());
  30.         System.out.println();
  31.  
  32.         // използваме GET() метода
  33.         System.out.println("Value at key = 1 : " + MyDictionary.get(1));
  34.         System.out.println("Value at key = 2 : " + MyDictionary.get(2));
  35.         System.out.println("Value at key = 3 : " + MyDictionary.get(3));
  36.         System.out.println("Value at key = 4 : " + MyDictionary.get(4));
  37.        System.out.println();
  38.  
  39.        // използваме GET() метода
  40.        System.out.println("Value at key = AP : " + MyDictionary.get("AP"));
  41.        System.out.println("Value at key = BA : " + MyDictionary.get("BA"));
  42.        System.out.println("Value at key = OR : " + MyDictionary.get("OR"));
  43.        System.out.println("Value at key = AV : " + MyDictionary.get("AV"));
  44.  
  45.         //принтираме преди да премахнем
  46.  
  47.         System.out.println();
  48.         System.out.println("Before removing elements : " + MyDictionary);
  49.         System.out.println();
  50.  
  51.         // For - cycle
  52.         System.out.println("By FOR-CYCLE :");
  53.         for (int i = 1; i <= MyDictionary.size() ; i++) {
  54.             System.out.println("MyDict element : " + MyDictionary.get(i));
  55.         }
  56.        System.out.println();
  57.  
  58.  
  59.         // KEYS() метод == принтиране
  60.         System.out.println("By Enumeration - KEYS :");
  61.         for (Enumeration items  = MyDictionary.keys(); items.hasMoreElements();) {
  62.             System.out.println("KEYS in Dictionary : " + items.nextElement());
  63.         }
  64.        System.out.println();
  65.  
  66.  
  67.         //VALUE() метод == принтиране
  68.         System.out.println("By Enumeration - VALUES :");
  69.         for (Enumeration items  = MyDictionary.elements(); items.hasMoreElements();) {
  70.             System.out.println("VALUE in Dictionary : " + items.nextElement());
  71.         }
  72.        System.out.println();
  73.  
  74.         // using isEmpty() method
  75.         System.out.println("\nIs my dictionary empty? : " + MyDictionary.isEmpty());
  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 1 and key 2
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement