Home  >  Core Java

Example of Dictionary in Java

By Arvind Rai, May 31, 2013
Dictionary is an abstract class which behaves as Map. Dictionary is legacy class. It implements Enumeration. Dictionary has two methods Dictionary.keys() and Dictionary.elements() that can be iterated by Enumeration. Dictionary is legacy class. We should use Map instead of Dictionary. Find example how to use Dictionary.
DictionaryDemo.java
package com.util;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
public class DictionaryDemo {
   public static void main(String[] args) {
       Dictionary<Integer, String> d = new Hashtable<Integer, String>(); 
       d.put(1,"Passion");
       d.put(2,"Motion");
       d.put(3,"Caution");
       Enumeration<Integer> key = d.keys();
       while(key.hasMoreElements()){
           System.out.println(key.nextElement());
       }
       Enumeration<String> element = d.elements();
       while(element.hasMoreElements()){
           System.out.println(element.nextElement());
       }
   }
}
Output
3
2
1
Caution
Motion
Passion 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us