A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-util-dictionary-class-java/ below:

Java Dictionary Class - GeeksforGeeks

Java Dictionary Class

Last Updated : 17 Dec, 2024

Try it on GfG Practice

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.

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

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 Disadvantages of Dictionary Class

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4