Dictionary Class in Java

In Java, a dictionary is a data structure that stores key-value pairs. The Java API provides the Dictionary class to implement a dictionary. The Dictionary class is an abstract class that defines a data structure for managing key-value pairs. It is the superclass of the Hashtable and Properties classes.

The Dictionary class is a legacy class, which means it was introduced in an earlier version of Java and has been replaced by newer classes. However, it is still supported and used in many legacy applications.

Features of Dictionary Class


The Dictionary class provides methods for adding, removing, and retrieving key-value pairs. Some of the important methods are:

  1. put(Object key, Object value): This method adds a key-value pair to the dictionary. If the key already exists in the dictionary, the value associated with the key is replaced with the new value.
  2. get(Object key): This method returns the value associated with the specified key. If the key does not exist in the dictionary, null is returned.
  3. remove(Object key): This method removes the key-value pair associated with the specified key from the dictionary.
  4. keys(): This method returns an Enumeration of the keys in the dictionary.
  5. elements(): This method returns an Enumeration of the values in the dictionary.

Creating a Dictionary Object


To use the Dictionary class, you must create an instance of it. However, the Dictionary class is an abstract class, so you cannot create an instance of it directly. Instead, you must create an instance of one of its concrete subclasses, such as Hashtable or Properties.

Hashtable Example:


Here is an example of creating a Hashtable object:

import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryExample {
public static void main(String[] args) {
Dictionary dict = new Hashtable<>();
dict.put("apple", "a fruit");
dict.put("car", "a vehicle");
System.out.println(dict.get("apple")); // prints "a fruit"
}
}


In this example, we create a Hashtable object and add two key-value pairs to it using the put() method. We then retrieve the value associated with the key “apple” using the get() method.

Properties Example:


Here is an example of creating a Properties object:

import java.util.Dictionary;
import java.util.Properties;

public class DictionaryExample {
public static void main(String[] args) {
Properties props = new Properties();
props.put("name", "John");
props.put("age", "30");
System.out.println(props.getProperty("name")); // prints "John"
}
}


In this example, we create a Properties object and add two key-value pairs to it using the put() method. We then retrieve the value associated with the key “name” using the getProperty() method.

Conclusion


The Dictionary class is a useful tool for managing key-value pairs in Java. While it is a legacy class, it is still supported and used in many legacy applications. The Dictionary class provides a way to add, remove, and retrieve key-value pairs, and it is the superclass of the Hashtable and Properties classes.