Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Dictionary;
- import java.util.Enumeration;
- import java.util.Hashtable;
- public class Main {
- public static void main(String[] args) {
- Dictionary <String , String> MyDictionary = new Hashtable();
- // Използване PUT метода
- // key : value
- /* MyDictionary.put(1,"Apple");
- MyDictionary.put(2,"Banana");
- MyDictionary.put(3,"Orange");
- MyDictionary.put(4,"Avocado"); */
- MyDictionary.put("AP","Apple");
- MyDictionary.put("BA","Banana");
- MyDictionary.put("OR","Orange");
- MyDictionary.put("AV","Avocado");
- // принтираме SIZE() на речника - преди да премахнем елементите
- System.out.println("\nSize of my_dict : " + MyDictionary.size());
- System.out.println();
- // използваме GET() метода
- System.out.println("Value at key = 1 : " + MyDictionary.get(1));
- System.out.println("Value at key = 2 : " + MyDictionary.get(2));
- System.out.println("Value at key = 3 : " + MyDictionary.get(3));
- System.out.println("Value at key = 4 : " + MyDictionary.get(4));
- System.out.println();
- // използваме GET() метода
- System.out.println("Value at key = AP : " + MyDictionary.get("AP"));
- System.out.println("Value at key = BA : " + MyDictionary.get("BA"));
- System.out.println("Value at key = OR : " + MyDictionary.get("OR"));
- System.out.println("Value at key = AV : " + MyDictionary.get("AV"));
- //принтираме преди да премахнем
- System.out.println();
- System.out.println("Before removing elements : " + MyDictionary);
- System.out.println();
- // For - cycle
- System.out.println("By FOR-CYCLE :");
- for (int i = 1; i <= MyDictionary.size() ; i++) {
- System.out.println("MyDict element : " + MyDictionary.get(i));
- }
- System.out.println();
- // KEYS() метод == принтиране
- System.out.println("By Enumeration - KEYS :");
- for (Enumeration items = MyDictionary.keys(); items.hasMoreElements();) {
- System.out.println("KEYS in Dictionary : " + items.nextElement());
- }
- System.out.println();
- //VALUE() метод == принтиране
- System.out.println("By Enumeration - VALUES :");
- for (Enumeration items = MyDictionary.elements(); items.hasMoreElements();) {
- System.out.println("VALUE in Dictionary : " + items.nextElement());
- }
- System.out.println();
- // using isEmpty() method
- System.out.println("\nIs my dictionary empty? : " + MyDictionary.isEmpty());
- // using REMOVE() method
- //remove value by Integer key 1 and key 2
- MyDictionary.remove(1);
- MyDictionary.remove(2);
- System.out.println();
- //remove value by String key 1 and key 2
- MyDictionary.remove("BA");
- MyDictionary.remove("AV");
- System.out.println();
- // CHECK BY INTEGER-KEY IF THE ELEMENTS ARE REMOVED
- System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(1));
- System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(2));
- System.out.println("Checking if the removed value exists (by integer key) : " + MyDictionary.get(3));
- System.out.println();
- // CHECK BY STRING-KEY IF THE ELEMENTS ARE REMOVED
- System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("BA"));
- System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("AV"));
- System.out.println("Checking if the removed value exists (by string key) : " + MyDictionary.get("AP"));
- System.out.println();
- // print after removing elements
- System.out.println("After removing elements : " + MyDictionary);
- System.out.println();
- // using size() method
- System.out.println("\nSize of my_dict after removing elements : " + MyDictionary.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement