Java Dictionary Class

Filed Under: Java

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<String, Integer> dict = new Hashtable<>();

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.

Map<String, String> tempMap = new HashMap<>();
tempMap.put("1", "One");

Dictionary<String, String> dict1 = new Hashtable<>(tempMap);
		
System.out.println(dict1); // prints {1=One}

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.

Dictionary<String, String> dict = new Hashtable<>();
dict.put("1", "One");
dict.put("2", "Two");
dict.put("3", "Three");

Enumeration<String> keys = dict.keys();

boolean found = false;
String lookupKey = "2";
while (keys.hasMoreElements()) {
	String key = keys.nextElement();
	if (lookupKey.contentEquals(key)) {
		found = true;
		System.out.println(lookupKey + " is present in the dictionary.");
		break;
	}
}
if (!found)
	System.out.println(lookupKey + " is not present in the dictionary.");

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.

String value = dict.get(lookupKey);

if(value != null)
	System.out.println(lookupKey + " is present in the dictionary.");
else
	System.out.println(lookupKey + " is not present in the dictionary.");

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.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages