Java.util.Dictionary Class in Java
The dictionary in Java resides in the abstract class util.Dictionary represents a data structure that stores key-value pairs, quickly retrieving a value associated with a given key. The Java standard library provides several implementations of dictionaries, such as HashMap, TreeMap, and LinkedHashMap. These implementations offer different performance characteristics and behaviours, catering to various needs in terms of insertion, retrieval, and iteration.
Dictionary Class in Java
It is a data structure that stores key-value pairs. The key is used for storing and retrieving the values and when required, can retrieve the element by using the key as a reference to that object or element. The direct subclass of a dictionary is Hashtable and maps the key to the value.
Note: In a dictionary, every key and its value are considered an object, and any non-null object is used as a key-value pair.
The dictionary has a hierarchy class, which is represented as follows:
In the dictionary, every key is linked with at most one value, as shown in the figure.
Syntax:
Example
Output
Dictionary Class Constructor
The syntax of the constructor:
A constructor in Java is used for the initialization of objects. It is invoked when an object of the class is created. It is used to set an initial value for attributes of objects in the java.
Dictionary Class Methods
Java dictionary class provides different methods. All the methods of the Dictionary class are abstract. The following table describes the methods.
Method | Description | Time Complexity |
---|---|---|
public abstract Enumeration elements() | In the dictionary, it returns the enumeration of the values. The enum object that is returned generates elements contained in the dictionary. | O(n) |
public abstract V get(Object key) | It returns the value of the key that is mapped in the dictionary. In the dictionary, a key is passed as an object. The associate value is returned if the entry is a specified key in the dictionary. Else, null is returned. If it's null, then NullPointerException is thrown. | O(n) |
public abstract boolean isEmpty() | This method checks whether a key to a value exists. If there are no entries, it returns true; otherwise, it returns false. | O(1) |
public abstract Enumeration keys() | In this dictionary, an enum of a key is returned. The enum object that is returned generates all the keys for the entries in the dictionary. | O(n) |
public abstract V put(K key, V value) | It inserts key-value pairs in the dictionary. The specific key is mapped to a specific value but neither of them should be null. If the entry for the specified key is already present, then the value is modified. If there is no entry for the specified key then it is created. | log(n) |
public abstract V remove(Object key) | The key which is to be removed is parsed. This method removes the key with its associated value. If there is no such key specified, then it throws NullPointerException. | log(n) |
public abstract int size() | It returns a number of keys in the dictionary. | O(1) |
Java Dictionary Program Examples
1. Dictionary.put Method
java.util.Dictionary.put(K key, V value) - it is used for adding the key-value pair in the dictionary.
Explanation: In this example, the method put takes the key and value from the user and puts it in the Hashtable.
2. Dictionary.elements() Method
java.util.Dictionary.elements() - It is used to return the value that is inside the dictionary.
Explanation: The values or the elements in the Hashtable are displayed by using the elements() method.
3. Dictionary.get Method
java.util.Dictionary.get(Object key) - used for returning the value that is mapped along with the key mentioned in the argument in the dictionary.
Explanation: The method 'get', retrieves the value at that specified key from the Hashtable.
4. Dictionary.isEmpty() Method
java.util.Dictionary.isEmpty() - it checks whether the dictionary is empty or not.
Explanation: It checks whether the Hashtable is empty or not.
5. Dictionary.keys() Method
java.util.Dictionary.keys() - it is used to return the representation of the key in the dictionary.
Example: It retrieves and prints all the keys in the dictionary.
6. Dictionary.size() Method
java.util.Dictionary.size() - it returns the key-value pair number.
Explanation: This method returns the size of Hashtable, i.e number of key0value pairs stored in the dictionary.
7. Dictionary.remove() Method
java.util.Dictionary.remove(Object key) removes the key-value pair from the dictionary.
Explanation: This method removes the specified object from the Hashtable.
Difference between the HashMap and Dictionary in Java
In Java, both HashMap and Dictionary are used to store key-value pairs, but they have some differences:
HashMap | Dictionary class |
---|---|
It implements a map interface. | It does not implement a map interface. |
It is a type of dictionary. | It is a dictionary. |
Used widely | It is obsolete |
Conclusion
- Java's util.Dictionary abstract class facilitates key-value relationships in data storage.
- It stores key-value pairs, enabling efficient retrieval and storage operations.
- Key functionalities include put, get, isEmpty, keys, size, and remove.
- Direct subclass Hashtable further implements this structure for mapping keys to values.
- Offers a powerful tool for organizing and managing data in Java applications.