Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Dictionary;
- import java.util.Enumeration;
- import java.util.Hashtable;
- public class Rechnik {
- public static void main(String[] args) {
- Dictionary<Integer, String> MyDictionary = new Hashtable<>();
- MyDictionary.put(1, "Apple");
- MyDictionary.put(2, "Bannana");
- MyDictionary.put(3, "Orange");
- MyDictionary.put(4, "Avocado");
- /*
- MyDictionary.put(AP, "Apple");
- MyDictionary.put(BA, "Bannana");
- MyDictionary.put(OR, "Orange");
- MyDictionary.put(AV, "Avocado");
- */
- System.out.println("\nSize of my dict: " + MyDictionary.size());
- 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("Before removing elements" + MyDictionary);
- 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("By enumeration-keys: ");
- for (Enumeration items = MyDictionary.keys(); items.hasMoreElements();) {
- System.out.println("Key in Dictionary: " + items.nextElement());
- }
- System.out.println("By enumeration-values: ");
- for (Enumeration items = MyDictionary.elements(); items.hasMoreElements();) {
- System.out.println("Value in Dictionary: " + items.nextElement());
- }
- System.out.println("\nIs my dictionary empty? : " + MyDictionary.isEmpty() + "\n");
- MyDictionary.remove(1);
- MyDictionary.remove(2);
- MyDictionary.remove("BA");
- MyDictionary.remove("AV");
- System.out.println("Cheking if the removed value exists(By Integer key): " + MyDictionary.get(1));
- System.out.println("Cheking if the removed value exists(By Integer key): " + MyDictionary.get(2));
- System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("BA"));
- System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("AV"));
- System.out.println("Cheking if the removed value exists(By String key): " + MyDictionary.get("AP"));
- System.out.println("After removing elements: " + MyDictionary);
- System.out.println("\nSize of my dict: " + MyDictionary.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement