Importance of a Dictionary class in Java?

JavaObject Oriented ProgrammingProgramming

A Dictionary class is an abstract class that represents a key/value pair and operates like a Map and it is a legacy class in Java. A Dictionary class has two important methods Dictionary.keys() and Dictionary.elements() that can be iterated by an Enumeration. The other important methods of a Dictionary class are  isEmpty(),  get(), remove() and size().

Syntax

public abstract class Dictionary<K,V> extends Object

Example

import java.util.*;
public class DictionaryTest {
   public static void main(String[] args) {
      Dictionary<Integer, String> dic = new Hashtable<Integer, String>();
      dic.put(1, "Adithya");
      dic.put(2, "Jaidev");
      dic.put(3, "Raja");
      Enumeration<Integer> key = dic.keys();
      while(key.hasMoreElements()) {
         System.out.println(key.nextElement());
      }
      Enumeration<String> element = dic.elements();
      while(element.hasMoreElements()) {
         System.out.println(element.nextElement());
      }
   }
}

Output

3
2
1
Raja
Jaidev
Adithya
raja
Updated on 02-Jul-2020 13:04:49

Advertisements