Open In App

Java Dictionary Class

Last Updated : 17 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Dictionary class in Java is an abstract class that represents a collection of key-value pairs, where keys are unique and used to access the values. It was part of the Java Collections Framework and it was introduced in Java 1.0 but has been largely replaced by the Map interface since Java 1.2.

  • Stores key-value pairs, where keys are unique.
  • Provides basic operations like insert, retrieve, and remove key-value pairs.
  • Keys and values are stored as Object.
  • Limited functionality compared to Map implementations.

Example 1:

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

public class Geeks {
    public static void main(String[] args) {
      
        // create a Dictionary instance 
        // using Hashtable
        Dictionary<String, Integer> d = new Hashtable<>();

        // Adding key-value pairs
        d.put("A", 25);
        d.put("B", 30);
        d.put("C", 35);

        // Retrieving a value using a key
        System.out.println("Value of B: " + d.get("B")); 

        // Replacing an existing value
        int oldValue = d.put("C", 40);
        System.out.println("Old Value of C: " + oldValue); 

        // Removing a key-value pair
        d.remove("A");

        // Displaying remaining key-value pairs
        Enumeration<String> k = d.keys();
        while (k.hasMoreElements()) {
            String key = k.nextElement();
            System.out.println("Key: " + key + ", Value: " + d.get(key));
        }
    }
}

Output
Value of B: 30
Old Value of C: 35
Key: C, Value: 40
Key: B, Value: 30

Explanation: In the above example, we have performed add, retrieved, replace, and removing key-value pairs using Dictionary with the implementation of Hashtable.

Declaration of Dictionary Class

public abstract class Dictionary<K, V> extends Object

  • Constructor: Dictionary(): The sole constructor for creating a Dictionary object.
  • Parameter: K is the type of keys and V is the type of values.
  • Return Type: Based on key-value operations, method returns values.

The Dictionary class has since been considered obsolete and its use is generally discouraged. This is because it was designed prior to the introduction of the Collections framework and does not implement the Map interface, which makes it difficult to use in conjunction with other parts of the framework.

In general, it’s recommended to use the Map interface or one of its implementations (such as HashMap or ConcurrentHashMap) instead of the Dictionary class.

Methods of Dictionary Class

Method

Description

Syntax

put(K key, V value)

It adds key-value pair to the dictionary. Returns the old value or null.

public abstract V put(K key, V value)

elements()

It returns value enumeration in dictionary.

public abstract Enumeration elements()

get(Object key)

It returns the value that is mapped with the key in the dictionary.

public abstract V get(Object key)

isEmpty()

It checks whether the dictionary is empty or not. 

public abstract boolean isEmpty()

keys()

It returns key representation in dictionary.

public abstract Enumeration keys()

remove(Object key)

It removes the key-value pair mapped with the key. Returns the value or null. 

public abstract V remove(Object key)

size()

It returns the total number of key-value pairs in the Dictionary.

public abstract int size()

Example 2: Performing all the Operations

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

public class Geeks {
    public static void main(String[] args) {

        // Create a Dictionary instance 
        // using Hashtable
        Dictionary<String, String> d = new Hashtable<>();

        // Add key-value pairs 
        d.put("Java", "1");
        d.put("Python", "2");

        print(d);

        // Retrieve a value using its key
        String v = d.get("Java");
        System.out.println("\nValue for key 'Java': " + v);

        // Check the number of key-value pairs
        System.out.println("Size of dictionary: " + d.size());

        // Check if the dictionary is empty
        System.out.println("Is dictionary empty? " + d.isEmpty());

        // Remove a key-value pair for given key 
        System.out.println("\nRemoving key 'Python'...");
        d.remove("Python");
        print(d);

        // Retrieve all keys 
        Enumeration<String> keys = d.keys();
        while (keys.hasMoreElements()) {
            String k = keys.nextElement();
            System.out.println("Key: " + k);
        }

        // Retrieve all values 
        Enumeration<String> values = d.elements();
        while (values.hasMoreElements()) {
            v = values.nextElement();
            System.out.println("Value: " + v);
        }
    }

    // Utility method to print all 
    // key-value pairs in the dictionary
    private static void print(Dictionary<String, String> d) {
        Enumeration<String> keys = d.keys();
        while (keys.hasMoreElements()) {
            String k = keys.nextElement();
            String v = d.get(k);
            System.out.println("Key: " + k + ", Value: " + v);
        }
    }
}

Output
Key: Java, Value: 1
Key: Python, Value: 2

Value for key 'Java': 1
Size of dictionary: 2
Is dictionary empty? false

Removing key 'Python'...
Key: Java, Value: 1
Key: Java
Value: 1

Advantages of Dictionary Class

  • Legacy Support: The Dictionary class was part of the original Java Collections framework and has been part of Java since the beginning. This means that if you have legacy code that uses Dictionary, you can still use it in your new code.
  • Simple to use: The Dictionary class is simple to use and provides basic key-value data structure functionality, which can be useful for simple cases.

Disadvantages of Dictionary Class

  • Obsolete: The Dictionary class is considered obsolete and its use is generally discouraged. This is because it was designed prior to the introduction of the Collections framework and does not implement the Map interface, which makes it difficult to use in conjunction with other parts of the framework.
  • Limited functionality: The Dictionary class provides basic key-value data structure functionality, but does not provide the full range of functionality that is available in the Map interface and its implementations.
  • Not type-safe: The Dictionary class uses the Object class to represent both keys and values, which can lead to type mismatches and runtime errors.


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg
  翻译: