Java Dictionary Class

Introduction

The Dictionary class in Java is a part of the java.util package.

The Dictionary class is an abstract class representing a key-value data structure.

It is the predecessor of the Map interface.

Table of Contents

  1. What is the Dictionary Class?
  2. Common Methods
  3. Usage and Limitations
  4. Conclusion

1. What is the Dictionary Class?

The Dictionary class provides a way to store key-value pairs, where keys are unique. It is an abstract class and has been mostly replaced by the Map interface and its implementations like HashMap.

2. Common Methods

  • put(K key, V value): Adds a key-value pair to the dictionary.
  • get(Object key): Retrieves the value associated with the specified key.
  • remove(Object key): Removes the key and its corresponding value.
  • size(): Returns the number of entries in the dictionary.
  • isEmpty(): Checks if the dictionary is empty.
  • keys(): Returns an enumeration of the keys.
  • elements(): Returns an enumeration of the values.

3. Usage and Limitations

  • The Dictionary class is abstract and must be extended to create concrete implementations.
  • It has been largely superseded by the Map interface and its implementations, which offer more functionality and flexibility.
  • Dictionary does not support generics, which can lead to type safety issues.

Example

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

public class DictionaryExample {
    public static void main(String[] args) {
        Dictionary<String, Integer> dictionary = new Hashtable<>();

        // Adding elements
        dictionary.put("Apple", 1);
        dictionary.put("Banana", 2);
        dictionary.put("Cherry", 3);

        // Retrieving elements
        System.out.println("Apple: " + dictionary.get("Apple"));

        // Removing an element
        dictionary.remove("Banana");
        System.out.println("After removing Banana: " + dictionary);

        // Checking size
        System.out.println("Size: " + dictionary.size());

        // Enumerating keys
        Enumeration<String> keys = dictionary.keys();
        System.out.println("Keys:");
        while (keys.hasMoreElements()) {
            System.out.println(keys.nextElement());
        }

        // Enumerating values
        Enumeration<Integer> values = dictionary.elements();
        System.out.println("Values:");
        while (values.hasMoreElements()) {
            System.out.println(values.nextElement());
        }
    }
}

Output:

Apple: 1
After removing Banana: {Cherry=3, Apple=1}
Size: 2
Keys:
Cherry
Apple
Values:
3
1

4. Conclusion

The Dictionary class in Java provides basic key-value storage but is mostly replaced by the Map interface and its implementations. It lacks features like type safety and modern concurrency controls, making Map a more versatile choice for most applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top