Table of contents
1.
Introduction
2.
Dictionary Class Methods in Java
2.1.
Example:
2.2.
Java
3.
Difference between the HashMap and Dictionary in Java
4.
Frequently Asked Questions
4.1.
What is a dictionary in Java?
4.2.
Does dictionary exist in Java?
4.3.
Is dictionary a data type in Java?
4.4.
How to get data from dictionary in Java?
5.
Conclusion
Last Updated: Jan 3, 2025
Easy

Java.util.Dictionary Class in Java

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Dictionary in Java is an abstract class that is a part of java.util package. It stores key-value pairs and works very similar to a Map. The Dictionary class is a direct superclass of HashTable, implemented by the Map interface. (See Data Structures)

Every key or value of a dictionary is an object and must not be null. Dictionary key objects have at most one value associated with them, using which elements can be accessed. Dictionary() is the sole constructor of the Dictionary class.

Declaration:

public abstract class Dictionary extends Object

 

Initialisation:

Dictionary dict_name = new Hashtable(); 
Java.util.Dictionary Class in Java

Dictionary Syntax in Java

public abstract class Dictionary<K,V> {     
	// Abstract methods and implementation	
 }

Dictionary Constructor

The Dictionary class constructor creates an empty dictionary. It's an abstract class that maps keys to values, though it's now obsolete - HashMap is the modern alternative.

Note: The Dictionary class has been deprecated since Java 1.2. It's recommended to use Map implementations like HashMap or TreeMap instead, as they provide better functionality and are part of the modern Collections Framework.

Dictionary Class Methods in Java

Method

Type

Description

isEmpty( )abstract BooleanIt checks if the dictionary has no key-value pairs.
put( K key, V value )abstract VIt maps the specified key and value in the dictionary and returns the previous value of the key or null if absent.
size( )abstract intIt returns the number of entries in the dictionary.
get( Object key )abstract VIt returns the value of the specified key.
remove( Object key )abstract VIt removes the specified key and its value from the dictionary and returns the value or null if absent.
keys( )abstract Enumeration<K>It returns an enumeration of all the keys in the dictionary.
elements( )abstract Enumeration<V>It returns an enumeration of all the values in the dictionary.

Also read, Duck Number in Java and  Hashcode Method in Java

Example:

The following example shows the use of all the functions of the Dictionary class mentioned in the table.

  • Java

Java

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

Dictionary dict = new Hashtable();

dict.put("A", "Australia");
dict.put("E", "Europe");

Enumeration continent = dict.elements();
System.out.print("Keys in Dictionary : ");
while(continent.hasMoreElements())
System.out.print(continent.nextElement()+ "  ");

   Enumeration letter = dict.keys();
System.out.print("\nValues in Dictionary : ");
while(letter.hasMoreElements())
System.out.print(letter.nextElement() + "  ");

System.out.println("\nValue at key = E : " + dict.get("E"));
System.out.println("Value at key = R : " + dict.get("R"));

System.out.println("Removing key E: " + dict.remove("E"));
  
   System.out.println("Is the dictionary empty? " + dict.isEmpty());

System.out.println("Size of Dictionary : " + dict.size());
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Keys in Dictionary : Australia  Europe  
Values in Dictionary : A  E  
Value at key = E : Europe
Value at key = R : null
Removing key E: Europe
Is the dictionary empty? false
Size of Dictionary : 1

 

The methods hasMoreElements() and nextElement() are used to check for more constants in the enumeration and get the next value respectively. Trying to get the value of key ‘R’ returns null as it does not exist in the dictionary. Try it on java online compiler.

Difference between the HashMap and Dictionary in Java

FeatureHashMap (Java)Dictionary (Java)
Packagejava.utiljava.util (Deprecated)
InheritanceExtends AbstractMap<K, V>Extends Object
Thread SafetyNot synchronized (Use ConcurrentHashMap for thread safety)Partially synchronized (some methods are synchronized)
PerformanceGenerally faster due to lack of synchronizationSlightly slower due to synchronized methods
Null Keys/ValuesAllows one null key and multiple null valuesDoes not allow null keys or values
IteratorsProvides Iterator interfaceUses Enumeration, which is older
ImplementationModern, frequently updatedLegacy class, not updated or used in new development
When to UsePreferred for most applications requiring key-value pairsGenerally avoided; use only in legacy codebases
ReplacementStandard Map implementations like HashMap and HashtableReplaced by HashMap and other Map implementations

Frequently Asked Questions

What is a dictionary in Java?

In Java, Dictionary is an abstract class representing a key-value data structure where each key is unique. It is part of java.util, but it is now considered obsolete and replaced by modern classes like HashMap and Hashtable.

Does dictionary exist in Java?

Yes, Dictionary exists in Java as a part of the java.util package. However, it is considered outdated and is rarely used in modern applications, where more efficient classes like HashMap are preferred for handling key-value pairs.

Is dictionary a data type in Java?

No, Dictionary is not a standalone data type in Java. It is an abstract class in the java.util package. Key-value pairs are typically managed with implementations of the Map interface, like HashMap and TreeMap.

How to get data from dictionary in Java?

To get data from a Dictionary, you use the get(Object key) method, passing the key for the value you want. However, since Dictionary is deprecated, it is better to use Map implementations like HashMap, where get works similarly.

Conclusion

Key-Value mappings are extremely useful for programmers to build applications. Maps, hash tables, and other related classes provide various functions to work with mapped data. The Map interface now replaces the Dictionary class but still holds good in the previous versions of the JDK. This blog discusses the Dictionary class and its features. It also lists out the different methods provided by this class for key-value mappings.

Recommended Readings:

Live masterclass