Advertisement
Ivan18113

Dictionary_withHashTable

Mar 20th, 2021
1,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /////////////////   DICTIONARY WITH HASHTABLE   /////////////////
  4.  
  5. public class Dictionary_Hashtable {
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner InputScanner = new Scanner(System.in);
  9.  
  10.         // РЕАЛИЗАЦИЯ НА Dictionary Чрез HashTable
  11.         // Dictionary предполага наличието на двойка <key : value>
  12.         Dictionary<Integer, String> MyDictionary = new Hashtable<>();
  13.  
  14.         //           key : value
  15.         MyDictionary.put(3,"MARCH");
  16.         MyDictionary.put(11,"NOVEMBER");
  17.         MyDictionary.put(8,"AUGUST");
  18.         MyDictionary.put(1,"JANUARY");
  19.         MyDictionary.put(4,"APRIL");
  20.         MyDictionary.put(10,"OCTOBER");
  21.         MyDictionary.put(7,"JULY");
  22.         MyDictionary.put(6,"JUNE");
  23.         MyDictionary.put(12,"DECEMBER");
  24.         MyDictionary.put(5,"MAY");
  25.         MyDictionary.put(9,"SEPTEMBER");
  26.         MyDictionary.put(2,"FEBRUARY");
  27.  
  28.  
  29.         System.out.println();
  30.  
  31.         //PRINT DICTIONARY SIZE: --------------------
  32.         int NumberOfElements = MyDictionary.size();
  33.         System.out.println("Number of elements: " + NumberOfElements);
  34.         System.out.println();
  35.  
  36.  
  37.         //PRINT DICTIONARY CONTENT: --------------------
  38.         System.out.println("Print whole dictionary: " + MyDictionary);
  39.         System.out.println();
  40.  
  41.         System.out.println("Изберете търсене по: 1.Ключ | 2. Стойност: ");
  42.         int choice = InputScanner.nextInt();
  43.  
  44.  
  45.         // Search by KEY =========================
  46.         if (choice == 1){
  47.  
  48.             Enumeration<Integer> itemsKeys = MyDictionary.keys();    // Дефинираме набор от КЛЮЧОВЕ.
  49.             Enumeration<String> itemsValue = MyDictionary.elements();     // Дефинираме набор от СТОЙНОСТИ.
  50.  
  51.  
  52.             System.out.print("Въведете номера на месеца: ");
  53.             String month_number = InputScanner.next();
  54.  
  55.             while(itemsKeys.hasMoreElements()){  // Правим итерации в/у КЛЮЧОВЕТЕ.
  56.  
  57.                 String currentKey = itemsKeys.nextElement().toString();    // Взимаме поредния КЛЮЧ.
  58.                 String currentValue = itemsValue.nextElement();     // Взимаме поредната СТОЙНОСТ.
  59.  
  60.                 if(currentKey.equals(month_number)){    // Проверяваме дали КЛЮЧА е равен на търсения.
  61.  
  62.                     System.out.println(currentValue);    // Разпечатваме неговата СТОЙНОСТ.
  63.                 }
  64.             }
  65.         }//end of choice 1
  66.  
  67.  
  68.         // Search by VALUE ===========================
  69.         if(choice == 2){
  70.  
  71.             Enumeration<Integer> itemsKeys = MyDictionary.keys();       // Дефинираме набор от КЛЮЧОВЕ
  72.             Enumeration<String> itemsValues = MyDictionary.elements();    // Дефинираме набор от СТОЙНОСТИ
  73.  
  74.  
  75.             System.out.println("Въведете името на месеца: ");
  76.             String month_name = InputScanner.next();
  77.  
  78.             while(itemsKeys.hasMoreElements()){ // Правим итерации в/у СТОЙНОСТИТЕ
  79.  
  80.                 String currentKey = itemsKeys.nextElement().toString();      // Взимаме поредния КЛЮЧ
  81.                 String currentValue = itemsValues.nextElement();        // Взимаме поредната СТОЙНОСТ
  82.  
  83.                 if(currentValue.equals(month_name)){  // Проверяваме дали СТОЙНОСТТА е равна на търсената
  84.  
  85.                     System.out.println(currentKey);    // Разпечатваме нейния КЛЮЧ.
  86.                 }
  87.             }
  88.         }//end of choice 2
  89.  
  90.     } //end of main
  91. } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement