Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ClassWorkDictionary {
- public static void main(String[] args) {
- Dictionary<Integer,String> myDictionary = new Hashtable<>();
- myDictionary.put(1,"Apple");
- myDictionary.put(2,"Banana");
- myDictionary.put(3,"Orange");
- myDictionary.put(4,"Mango");
- System.out.println("\nSize of my dictionary: " + 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);
- for(int i = 1; i <= myDictionary.size(); i++){
- System.out.println("MyDict element : " + myDictionary.get(i));
- }
- System.out.println("By Enumerator - KEYS: ");
- for(Enumeration<Integer> item = myDictionary.keys(); item.hasMoreElements();){
- System.out.println("Keys in dictionary " + item.nextElement());
- }//end of Em(Integer)
- System.out.println("By Enumerator - ELEMENTS");
- for(Enumeration<String> item = myDictionary.elements(); item.hasMoreElements();){
- System.out.println("Elements in dictionary " + item.nextElement());
- }//end of Em(String)
- System.out.println("\nIs my dictionary empty?: " + myDictionary.isEmpty() + "\n");
- myDictionary.remove(1);
- myDictionary.remove(2);
- System.out.println("Checking if the removed value exists: " + myDictionary.get(1));
- System.out.println("Checking if the removed value exists: " + myDictionary.get(2));
- System.out.println("After removing elements:" + myDictionary);
- System.out.println("\nSize of my dict: " + myDictionary.size());
- }//end of main
- }//end of class
Advertisement
Add Comment
Please, Sign In to add comment