Dictionary is an abstract class in java.util (predecessor of Map interface).It represents a key-value pair data structure.It is obsolete and rarely used in favor of the Map interface (especially HashMap).Hashtable is a concrete subclass of Dictionary.
Commonly Used Constructors and Methods


Note: Dictionary uses Enumeration, not Iterator.
Simple Program — Dictionary via Hashtable
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Enumeration;
public class DictionaryExample {
public static void main(String[] args) {
Dictionary<String, String> countryCapital = new Hashtable<>();
countryCapital.put("India", "New Delhi");
countryCapital.put("USA", "Washington D.C.");
countryCapital.put("Japan", "Tokyo");
System.out.println("Country-Capital Pairs:");
Enumeration<String> keys = countryCapital.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.println(key + " -> " + countryCapital.get(key));
}
}
}Output
Country-Capital Pairs:
Japan -> Tokyo
USA -> Washington D.C.
India -> New DelhiCode language: CSS (css)
Problem Statement:
Lokesh and Balaji are designing a user role management system for an internal bank tool. They want to map usernames to roles (e.g., admin, manager, teller) using java.util.Dictionary. The system must:
- Store predefined users and their roles
- Allow querying a user’s role
- Print all usernames and roles
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Scanner;
public class UserRoleManager {
public static void main(String[] args) {
Dictionary<String, String> userRoles = new Hashtable<>();
// Predefined users
userRoles.put("lotusjavaprince", "Admin");
userRoles.put("mahesh", "Manager");
userRoles.put("anita", "Teller");
userRoles.put("rajesh", "Customer Support");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username to check role: ");
String username = scanner.nextLine().toLowerCase();
if (userRoles.get(username) != null) {
System.out.println("Role of " + username + " is: " + userRoles.get(username));
} else {
System.out.println("User not found!");
}
System.out.println("\nAll User-Role Pairs:");
Enumeration<String> keys = userRoles.keys();
while (keys.hasMoreElements()) {
String user = keys.nextElement();
String role = userRoles.get(user);
System.out.println(user + " -> " + role);
}
scanner.close();
}
}Enter username to check role: Mahesh
Role of mahesh is: Manager
All User-Role Pairs:
rajesh -> Customer Support
anita -> Teller
mahesh -> Manager
lotusjavaprince -> Admin
The java.util.Dictionary class is a legacy abstract class designed for handling key-value pairs, similar in purpose to modern Map interfaces. While once widely used, it has now been largely replaced by more robust, efficient, and flexible alternatives like HashMap and LinkedHashMap.
