Javatpoint Logo
Javatpoint Logo

Dictionary Class in Java

In Java, Dictionary is the list of key-value pairs. We can store, retrieve, remove, get, and put values in the dictionary by using the Java Dictionary class. In this section, we will discuss the Java Dictionary class that stores data in key-value pairs just like the Map interface.

Java Dictionary Class

Java Dictionary class is an abstract class parent class of any class. It belongs to java.util package. Its direct known subclass is the Hashtable class. Like the Hashtable class, it also maps the keys to values. Note that every key and value is an object and any non-null object can be used as a key and as a value. The Dictionary class hierarchy is as follows:

Dictionary Class in Java

Every key is associated with at most one value, as shown in the following figure. Once the value is stored in a dictionary object, we can retrieve it by using the key.

Dictionary Class in Java


Note: The class is obsolete. So, implement the map interface instead of extending the class.

Dictionary Class Constructor

The class has only a constructor called a sole constructor.


Dictionary Class Methods

All the methods of the Dictionary class are abstract. The following table describes the methods.

Method Description
public abstract Enumeration elements() It returns an enumeration of the values in this dictionary. The returned enum object generates all the elements contained in entries in this dictionary.
public abstract V get(Object key) It returns the value to which the key is mapped in this dictionary. It parses an object (key) in this dictionary. Note that if this dictionary contains an entry for the specified key, the associated value is returned; otherwise, null is returned. It throws NullPointerException if the key is null.
public abstract boolean isEmpty() The method checks if this dictionary maps no keys to value. It returns true if and only if this dictionary contains no entries, else returns false.
public abstract Enumeration keys() It returns an enum of the keys in this dictionary. The returned enum object generates all the keys for which this dictionary contains entries.
public abstract V put(K key, V value) The method is used to insert key-value pair in the dictionary. It maps the specified key to the specified value in this dictionary. Note that neither key nor value can be null.
If the dictionary already contains an entry for the specified key, the value already in this dictionary for that key is returned, after modifying the entry to contain the new element.
If the dictionary does not already have an entry for the specified key, an entry is created for the specified key and value, and null is returned.
It parses key and value as a parameter. It throws NullPointerException if the key or value is null.
public abstract V remove(Object key) The method parses a key that we want to remove. It removes the key and associated value. Note that the method does nothing if the key is not in the dictionary. It throws NullPointerException if the key is null.
public abstract int size() It returns the number of entries (distinct keys) in this dictionary.

Java Dictionary Programs

Use of Dictionary.put() Method

The put() method inserts the elements in the dictionary. The following program demonstrates the same.


{108=Canberra, 107=Nelson Bay, 106=Mount Gambier, 105=Lismore, 104=Perth, 103=Melbourne, 102=Brisbane, 101=Sydney}

Use of Dictionary.size() Method

The size of the dictionary is the number of elements the dictionary contains. In the following program, the size of the dictionary is 6.


The size of the dictionary is: 6

Use of Dictionary.get() Method

By using the get() method, we can retrieve the value of a specified key.


The value of the specified key is: Melbourne

Use of Dictionary.isEmpty() Method

It returns true if dictionary is empty, else returns false.


Is the dictionary empty? 

Use of Dictionary.remove() Method

The method removes the key and corresponding value that we have parsed in the method. The removed value is returned by the method.


The removed value is: Mount Gambier

Use of elements() and key() Methods


Dictionary values are: 

Nelson Bay
Mount Gambier

Dictionary keys are: 


Difference Between the HashMap and Dictionary Class

The Java HashMap class and the Dictionary class both perform a similar function. The only difference is that HashMap implements the Map Interface while the Dictionary class does not. According to Java documentation, the Dictionary class is no longer in use because it is outdated. Instead of the Dictionary class, the HashMap class is used. Since we can say that HashMap is a type of dictionary.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA