Java Dictionary Class With Examples

Java Dictionary is an abstract class. It was the parent class for any key-value mapping objects, such as Hashtable. However, it got deprecated in favor of the Map interface introduced in Java 1.2, which later on streamlined the Collections Framework. Dictionary doesn’t allow null for key and value.

Note: Dictionary class has been obsolete and you shouldn’t use it. I use dictionary a lot in Python and got curious if there is a Dictionary class in Java? That’s how I got to know about the Dictionary class. The information provided here is just to have some idea about it if you are curious, try to avoid using it in your application.

Java Dictionary Methods

This class declares 7 methods, which the implementation classes had to implement.

  1. int size(): returns the size of the dictionary.
  2. boolean isEmpty(): returns true if there are no key-value mappings, else false.
  3. Enumeration<K> keys(): returns the enumeration of the keys in the dictionary.
  4. Enumeration<K> elements(): returns the enumeration of the values in the dictionary.
  5. V get(Object key): returns the value associated with the key, if the key doesn’t exist then returns null.
  6. V put(K key, V value): adds the key-value pair to the dictionary. If any of the key-value is null, then throws NullPointerException. If the key already exists, then the value associated is returned and then the new value is updated. If it’s a new key, then null is returned.
  7. V remove(Object key): removes the key-value pair from the dictionary. The value associated with the key is returned. If the key doesn’t exist in the dictionary, then do nothing and null is returned.

Dictionary Implementation Classes

The only direct implementation of Dictionary is the Hashtable class. The Properties class extends Hashtable, so that is also an implementation of the Dictionary.

Java Dictionary Initialization

Dictionary support Generics, so we can specify the key-value types while declaring and instantiating the Dictionary object.

Dictionary Initialization with Values

The Hashtable class has a constructor that accepts a Map and copy its key-pair to the Hashtable object. We can use it to initialize a dictionary with values.

Java Dictionary vs Map

  • Dictionary is an abstract class whereas Map is an interface.
  • Dictionary class has been deprecated when Collection classes were streamlined and Map got introduced in JDK 1.2
  • Don’t use Dictionary in your applications, it’s better to use Map.

Java Dictionary vs Hashtable

  • Dictionary is an abstract class where as Hashtable is the implementation of Dictionary.
  • Dictionary class has been deprecated whereas Hashtable is still being used. In fact, Hashtable is part of Collections framework and implements Map interface.

How to Check if a Key Exists in Dictionary

Here is a simple program where we are iterating over the enumeration of keys to check if the key exists in the dictionary or not.

We can also use the get() method to check if the key exists or not. If key doesn’t exists, then null is returned. Also, null values are not allowed, so it’s safe to use the null check for this.

Conclusion

Dictionary is an obsolete class, so you shouldn’t use it in your application. You might be tempted to use it if you are coming from Python background, but avoid it and use Map and their implementation classes.

By admin

Leave a Reply

%d bloggers like this: