Dictionary in Java is an abstract class in Java that stores the data in the form of key-value pairs. It is found in the java.util
package and works similar to a Map. Each key has a value and we can retrieve the values in a Dictionary object using its corresponding key.
Table of Contents
Declaration of a Dictionary class
We can declare a dictionary object in Java in the below way:
public abstract class Dictionary extends Object
Initializing a Dictionary object
To initialize a dictionary object, we can use either a Hashtable
, HashMap
or LinkedHashMap
.
Dictionary<Type,Type> obj = new Hashtable<Type,Type>();
obj – Dictionary object
Type – represents the type of key and value
Every key has a corresponding value and both the key and value cannot have null values.
The below diagram shows the key-value pair association.
Java Dictionary Methods
Below are the methods present in the Dictionary class in Java:
Method | Description | Parameter |
---|---|---|
Enumeration elements() | Returns an enumeration of values in the dictionary | |
Object get(Object key) | Returns the value associated with the specified key in the dictionary | key - the key to be searched to retrieve the corresponding value |
boolean isEmpty() | Checks whether the dictionary has no key-value pairs | Returns false if there is key-value pair Returns true if there are no key-value pairs |
Enumeration keys() | Returns an enumeration of keys in the dictionary | |
Object put(Object key, Object value) | Maps a specific key to a value in the dictionary | key - hashtable key value - value to be associated with the key |
Object remove(Object key) | Removes the key and its value from the dictionary | key - the key to be retrieved |
int size() | Returns the number of distinct keys in the dictionary |
Example of Java Dictionary methods
In the below example, we have used all the methods of the Dictionary class in Java. To use the Dictionary class, we need to import the java.util.Dictionary package.
The put()
method inserts the key-value pair data into the Dictionary object. To retrieve the size, we use the size()
method. The get()
method retrieves the value of the corresponding key which is passed as a parameter.
We can iterate through the dictionary elements using the enumeration
to get all the values. To get all the keys, we use the keys()
method.
To remove a key-value pair, we use the remove()
method.
When there is already a key-value pair present, it replaces the value when we use the put()
method. In the below example, it replaces the value “Kavitha” with “Kavya”.
The isEmpty()
method checks if the Dictionary is empty.
import java.util.Dictionary; import java.util.Enumeration; import java.util.Hashtable; public class DictionaryDemo { public static void main(String[] args) { Dictionary<Integer, String> dic = new Hashtable<Integer, String>(); dic.put(1, "Ramesh"); dic.put(2, "Suresh"); dic.put(3, "Kavitha"); dic.put(4, "Ramya"); dic.put(5, "Jay"); System.out.println("Size of the dictionary: " + dic.size()); System.out.println("Value of the key 3: " + dic.get(3)); //Iterate over elements in dictionary System.out.println("Iterating using enumeration:"); Enumeration<String> e = dic.elements(); while(e.hasMoreElements()) System.out.print(e.nextElement() + " "); System.out.println("\nDisplay the keys:"); Enumeration<Integer> ekey = dic.keys(); while(ekey.hasMoreElements()) System.out.print(ekey.nextElement() + " "); dic.remove(4); System.out.println("\nValues after removing element at key 4:" + dic); //Update value at key 3 dic.put(3, "Kavya"); System.out.println("Value at index 3: " + dic.get(3)); System.out.println("Is dictionary empty: " + dic.isEmpty()); } }
Size of the dictionary: 5 Value of the key 3: Kavitha Iterating using enumeration: Jay Ramya Kavitha Suresh Ramesh Display the keys: 5 4 3 2 1 Values after removing element at key 4:{5=Jay, 3=Kavitha, 2=Suresh, 1=Ramesh} Value at index 3: Kavya Is dictionary empty: false
Note: The Dictionary class is obsolete and we need to implement the Map interface for the key-value functionality.
Conclusion
In this tutorial, we have seen how to use the Dictionary class to store the key-value pairs by implementing using the Hashtable. We have also seen how to use the various methods present in the Dictionary class.