Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- ///////////////// DICTIONARY WITH HASHTABLE /////////////////
- public class Dictionary_Hashtable {
- public static void main(String[] args) {
- Scanner InputScanner = new Scanner(System.in);
- // РЕАЛИЗАЦИЯ НА Dictionary Чрез HashTable
- // Dictionary предполага наличието на двойка <key : value>
- Dictionary<Integer, String> MyDictionary = new Hashtable<>();
- // key : value
- MyDictionary.put(3,"MARCH");
- MyDictionary.put(11,"NOVEMBER");
- MyDictionary.put(8,"AUGUST");
- MyDictionary.put(1,"JANUARY");
- MyDictionary.put(4,"APRIL");
- MyDictionary.put(10,"OCTOBER");
- MyDictionary.put(7,"JULY");
- MyDictionary.put(6,"JUNE");
- MyDictionary.put(12,"DECEMBER");
- MyDictionary.put(5,"MAY");
- MyDictionary.put(9,"SEPTEMBER");
- MyDictionary.put(2,"FEBRUARY");
- System.out.println();
- //PRINT DICTIONARY SIZE: --------------------
- int NumberOfElements = MyDictionary.size();
- System.out.println("Number of elements: " + NumberOfElements);
- System.out.println();
- //PRINT DICTIONARY CONTENT: --------------------
- System.out.println("Print whole dictionary: " + MyDictionary);
- System.out.println();
- System.out.println("Изберете търсене по: 1.Ключ | 2. Стойност: ");
- int choice = InputScanner.nextInt();
- // Search by KEY =========================
- if (choice == 1){
- Enumeration<Integer> itemsKeys = MyDictionary.keys(); // Дефинираме набор от КЛЮЧОВЕ.
- Enumeration<String> itemsValue = MyDictionary.elements(); // Дефинираме набор от СТОЙНОСТИ.
- System.out.print("Въведете номера на месеца: ");
- String month_number = InputScanner.next();
- while(itemsKeys.hasMoreElements()){ // Правим итерации в/у КЛЮЧОВЕТЕ.
- String currentKey = itemsKeys.nextElement().toString(); // Взимаме поредния КЛЮЧ.
- String currentValue = itemsValue.nextElement(); // Взимаме поредната СТОЙНОСТ.
- if(currentKey.equals(month_number)){ // Проверяваме дали КЛЮЧА е равен на търсения.
- System.out.println(currentValue); // Разпечатваме неговата СТОЙНОСТ.
- }
- }
- }//end of choice 1
- // Search by VALUE ===========================
- if(choice == 2){
- Enumeration<Integer> itemsKeys = MyDictionary.keys(); // Дефинираме набор от КЛЮЧОВЕ
- Enumeration<String> itemsValues = MyDictionary.elements(); // Дефинираме набор от СТОЙНОСТИ
- System.out.println("Въведете името на месеца: ");
- String month_name = InputScanner.next();
- while(itemsKeys.hasMoreElements()){ // Правим итерации в/у СТОЙНОСТИТЕ
- String currentKey = itemsKeys.nextElement().toString(); // Взимаме поредния КЛЮЧ
- String currentValue = itemsValues.nextElement(); // Взимаме поредната СТОЙНОСТ
- if(currentValue.equals(month_name)){ // Проверяваме дали СТОЙНОСТТА е равна на търсената
- System.out.println(currentKey); // Разпечатваме нейния КЛЮЧ.
- }
- }
- }//end of choice 2
- } //end of main
- } //end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement