Ivan18113

ClassWorkDictionary

Mar 22nd, 2021 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class ClassWorkDictionary {
  5.     public static void main(String[] args) {
  6.  
  7.         Dictionary<Integer,String> myDictionary = new Hashtable<>();
  8.  
  9.         myDictionary.put(1,"Apple");
  10.         myDictionary.put(2,"Banana");
  11.         myDictionary.put(3,"Orange");
  12.         myDictionary.put(4,"Mango");
  13.  
  14.         System.out.println("\nSize of my dictionary: " + myDictionary.size());
  15.  
  16.         System.out.println("Value at key = 1 " + myDictionary.get(1));
  17.         System.out.println("Value at key = 2 " + myDictionary.get(2));
  18.         System.out.println("Value at key = 3 " + myDictionary.get(3));
  19.         System.out.println("Value at key = 4 "  + myDictionary.get(4));
  20.  
  21.         System.out.println("Before removing elements: " + myDictionary);
  22.  
  23.         for(int i = 1; i <= myDictionary.size(); i++){
  24.  
  25.             System.out.println("MyDict element : " + myDictionary.get(i));
  26.         }
  27.  
  28.         System.out.println("By Enumerator - KEYS: ");
  29.         for(Enumeration<Integer> item = myDictionary.keys(); item.hasMoreElements();){
  30.             System.out.println("Keys in dictionary " + item.nextElement());
  31.  
  32.         }//end of Em(Integer)
  33.  
  34.         System.out.println("By Enumerator - ELEMENTS");
  35.         for(Enumeration<String> item = myDictionary.elements(); item.hasMoreElements();){
  36.             System.out.println("Elements in dictionary " + item.nextElement());
  37.  
  38.         }//end of Em(String)
  39.  
  40.         System.out.println("\nIs my dictionary empty?: " + myDictionary.isEmpty() + "\n");
  41.         myDictionary.remove(1);
  42.         myDictionary.remove(2);
  43.         System.out.println("Checking if the removed value exists: " + myDictionary.get(1));
  44.         System.out.println("Checking if the removed value exists: " + myDictionary.get(2));
  45.         System.out.println("After removing elements:" + myDictionary);
  46.         System.out.println("\nSize of my dict: " + myDictionary.size());
  47.  
  48.     }//end of main
  49. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment