Dictionary class in java – Java Dictionary Class with Example | Java.util.Dictionary Class in Java | Definition, Syntax, Methods & Sample Code

Dictionary class in java: Dictionary is an abstract class that provides a key/value storage repository and works much like Map. In this tutorial, you will review and learn about the Java Dictionary Abstract Class. By using an example you’ll easily understand the demonstration of the Dictionary usage. Have an idea about the java dictionary class definition, declaration, hierarchy, methods via direct links available below:

Java.util.Dictionary Class in Java

Java dictionary example: Java Dictionary is an abstract class, representing a key-value relation and works similar to a map. If we have a key-value pair then we can store the value in the Dictionary object. Once the value is stored in a dictionary object, we can retrieve it by using its key. In Dictionary, any non-null object can be used as a key and as a value.

dictionary class in java

Creating a Dictionary

Java dictionary example: The primary step to create a dictionary in Java is to select a class that performs a “key-value pair” interface; a few instances involve HashTables, HashMap, and LinkedHashMap.

The Dictionary object classes are implemented in java.utils package.

Declaration of Dictionary Class in java

public abstract class Dictionary extends Object

Constructors of Java Dictionary

  • Dictionary(): Sole constructor.

Java Dictionary Class Hierarchy

A Diagrammatic illustration of the Dictionary class Hierarchy is as below:

dictionary hierarchy

Read More: 

Methods of Java Dictionary

The abstract methods specified by Java Dictionary are mentioned below:

  1. Object put(Object key, Object value): This method is used to insert a key/value pair into a Dictionary.
  2. Object get(Object key): It returns the object that contains the value associated with the key.
  3. boolean isEmpty(): It checks whether the Dictionary is empty or not. If the Dictionary is empty it will return true else return false.
  4. int size(): Returns the number of key/value pair in the dictionary.
  5. Object remove(Object key): Removes the key and its value. Declares the value associated with the key. If the key is not in the dictionary, it returns null.
  6. Enumeration elements(): This method returns an enumeration of the values contained in the dictionary.
  7. Enumeration keys(): This method returns an enumeration of the keys contained in the dictionary.

Java Dictionary Example

import java.util.*;
class DictionaryExample{
public static void main(String args[]){

//creating a dictionary
Dictionary dict = new Hashtable();

//adding values in this dictionary
dict.put(101, "Rahul");
dict.put(109, "Amit");
dict.put(103, "Vijay");
dict.put(102, "Suresh");
dict.put(108, "Prashant");

//returns size of the dictionary
System.out.println("Dictionary size is: "+dict.size());

//enumeration of the values contained in the dictionary.
for(Enumeration enm = dict.elements(); enm.hasMoreElements();){
System.out.println("Dictionary values are: "+enm.nextElement());

//returns the value at key 108.
System.out.println("Retrieve value at key 108: " +dict.get(108));

//returns a boolean value
System.out.println("Check dictionary is empty or not: " +dict.isEmpty());

//enumeration of the keys contained in the dictionary.
for(Enumeration enm = dict.keys(); enm.hasMoreElements();){
System.out.println("Dictionary keys are: "+enm.nextElement());


Java Dictionary Class with Example