Core Java

Java.util.Dictionary Class – Java Dictionary Example

1. Introduction

In this example, we will learn about java.util.Dictionary class. The Dictionary class is the abstract parent of any class which maps keys to values e.g. Hashtable. In a Dictionary object, every key is associated with at most one value.

Using the Dictionary object and the key we can lookup for the respective element. Any non-null object can be used as a key and a value. The equals  method should be used by implementations of this class to decide if two keys are the same. NullPointerException is thrown in case if any null key or null value is tried to be inserted in a Dictionary object.

2. Syntax

The following snippet shows the syntax of the Dictionary class.

syntax

 public abstract class Dictionary extends Object 

3. Hierarchy

A brief diagrammatic representation of the Dictionary class is as follows:

Diagrammatic representation of the Dictionary class Hierarchy

As we can see from the above diagram Hashtable is the direct subclass of the Dictionary class.

4. Constructor and Description

Dictionary(): It’s the default constructor and is the sole constructor for the class. Since Dictionary class is abstract hence it cannot be instantiated directly. We need to create the object of the subclass i.e. Hashtable while creating the instance of the Dictionary class as shown in the below code:

 Dictionary object =  new Hashtable(); 

In the above code, we can see while instantiating the Dictionary class the object of the Hashtable class is created thereby calling the constructor of the Hashtable class which in turn calls the constructor of the parent class which is the Dictionary class itself.

5. Methods Summary

The table shows all the methods and their description for the Dictionary class.

Method SyntaxDescriptionReturn valueException Thrown
public abstract Enumeration<V> elements()Returns an enumeration that contains all the elements within the dictionary. An enumeration of the values in this dictionary.
public abstract V get(Object key) Returns the value within the Dictionary with which the given key is mapped otherwise null is returned. the value to which the key is mapped in this dictionary; NullPointerException – if the key
 is null
.
public abstract boolean isEmpty() Checks if the Dictionary maps no keys to values i.e. it doesn’t have any entries. true if this dictionary maps no keys to values; false otherwise.
public abstract Enumeration<K> keys()Returns an enumeration for all the keys in the dictionary. an enumeration of the keys in this dictionary.

public abstract V put(K key, V value)Maps the specified key to the specified value in this dictionary and returns null if the dictionary doesn’t have similar entry otherwise returns the pre-existing value for the key if already an entry for that key is present in the dictionary and also updates the entry with the new value. the previous value to which the key was mapped in this dictionary, or null if the key did not have a previous mapping.

NullPointerException – if the key or value is null.
public abstract V remove(Object key) Removes the key (and its corresponding value) from this dictionary. This method does nothing if the keyis not in this dictionary. the value to which the key had been mapped in this dictionary, or null if the key did not have a mapping. NullPointerException – if the key is null.
public abstract int size() Returns the number of entries (distinct keys) in this dictionary. the number of keys in this dictionary.

6. Java Dictionary Example

We will now see a demo example for the Java Dictionary class to demonstrate the usage of the methods of the Dictionary class.

DictionaryDemo.java

// Java Program to demonstrate the use of different methods of java.util.Dictionary class.
  
import java.util.*; 
public class DictionaryDemo 
{ 
    public static void main(String[] args) 
    {   
        // Creating a Dictionary Object 
        Dictionary  myDictionary = new Hashtable(); 
  
        // using the put() method 
        myDictionary.put("1", "Hello"); 
        myDictionary.put("2", "World"); 
  
        // using the elements() method
        for (Enumeration en = myDictionary.elements(); en.hasMoreElements();) 
        { 
            System.out.println("Value in Dictionary: " + en.nextElement()); 
        } 
  
        // using the get() method
        System.out.println("\nValue at key = 3: " + myDictionary.get("3")); 
        System.out.println("Value at key = 2: " + myDictionary.get("2")); 
  
        // using the isEmpty() method : 
        System.out.println("\nThere is no key-value pair: " + myDictionary.isEmpty() + "\n"); 
  
        // using the keys() method : 
        for(Enumeration k = myDictionary.keys(); k.hasMoreElements();) 
        { 
            System.out.println("Keys in Dictionary: " + k.nextElement()); 
        } 
  
        // using the remove() method : 
        System.out.println("\nRemove : " + myDictionary.remove("2")); 
        System.out.println("Check the value of the removed key: " + myDictionary.get("2")); 
  
        System.out.println("\nSize of Dictionary: " + myDictionary.size()); 
  
    } 
}

After running the above code in any IDE of your choice you’ll receive the following output:

Output

Value in Dictionary: World
Value in Dictionary: Hello

Value at key = 3 : null
Value at key = 2 : World

There is no key-value pair: false

Keys in Dictionary: 2
Keys in Dictionary: 1

Remove: World
Check the value of the removed key:null

Size of Dictionary:1

In the given example we have seen a sample usage of the get, put, elements, isEmpty, keys and remove methods.

7. Conclusion

This Java Dictionary class is obsolete. For any newer classes to implement the concept of Dictionary class, they need to implement the Java Map interface, e.g. use the HashMap Class, rather than extending the Dictionary class.

8. References

9. Download the Source Code

This was an example of the Java Dictionary class.

Download
You can download the full source code of this example here: Java.util.Dictionary Class – Java Dictionary Example

Gaurav Verma

I am Gaurav Verma from Kolkata, India. I have done Bachelors of Technology in Computer Science & Engineering from Narula Institute of Technology under West Bengal University of Technology. I joined NIIT LTD. as a Technical Trainer in 2007 and trained students pursuing GNIIT and Bsc.IT curriculum as well as modular courses in various technologies like Java, .Net and Open Source technologies. I have been a very enthusiastic Java programmer and take lot of interest in programming through Java language and also develop Standalone and web applications using Java technologies. My favourite pass time are Solving Puzzles, Sudoku, watching documentaries etc.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Avi Abrami
Avi Abrami
4 years ago

From the javadoc for class Dictionary: NOTE: This class is obsolete. New implementations should implement the Map interface, rather than extending this class.
From the Javadoc for class Hashtable: If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

Back to top button