- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Dictionary Class
Introduction
The Java Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. The Dictionary class was part of the early Java platform since JDK 1.0.
Characterstics
The following are the important points about the Dictionary class in Java −
- In this class, every key and every value is an object.
- In this class, every key object is associated with at most one value.
- Keys and values can not be Null. It will throw a NullPointerException error if we try to add a Null key and values.
Class Declaration
The following is the declaration for java.util.Dictionary class −
public abstract class Dictionary<K,V> extends Object
Parameters
- K − Here, K represents the type of key to be stored in the Dictionary.
- V − Here, V represents the type of values to be stored in the Dictionary.
Why was the Dictionary Class Replaced?
The Dictionary class is declared obsolete in its documentation with the introduction of the Java Collections Framework in Java 1.2. This change was introduced to handle groups of objects more easily, and the Collections Framework provided it with a hierarchical structure under interfaces in Java.
The main reason for its depreciation is the Map interface. The Map interface provided different implementations like HashMap, TreeMap, or LinkedHashMap, and as the Dictionary class is dependent on the abstract class, it doesn't provide other implementations.
The Dictionary class is dependent on enumeration for the iteration of key-value pairs, but in the Map interface iterator class was introduced, which is more efficient in iterating as well as removing elements during iteration which is not supported in the Dictionary Class.
Creating a Dictionary Class
To create a Dictionary class in Java, follow the steps as follows −
Step 1
The first and important step is to import the java.util.Dictionary and java.util.Hashtable at the start of the code, as without the Dictionary and Hashtable classes, we cannot use their methods.
import java.util.Dictionary; import java.util.Hashtable;
Step 2
The second step is to create the object of the Dictionary class. Here, the "dict" is the Dictionary class object, as we know that it is an abstract class, so to instantiate it, we use a hashtable for its creation.
Dictionary<Integer, String> dict = new Hashtable<>();
The above Dictionary consists of keys as Integers and values as strings.
Step 3
The third step is to add key-value pairs using the put() method. We will be adding two entries in the dict as (10, "Orange") and (20, "Mango").
dict.put(10, "Orange"); dict.put(20, "Mango");
Example
Below is an example to create a Dictionary in Java −
package com.tutorialspoint; import java.util.Dictionary; import java.util.Hashtable; public class DictionaryDemo { public static void main(String[] args) { // creating a dictionary with a hashtable Dictionary<Integer, String> dict = new Hashtable<>(); // adding pairs using the put() method dict.put(10, "Orange"); dict.put(20, "Mango"); // printing the dictionary System.out.println(dict); } } Output
Let us compile and run the above program. This will produce the following result −
{10=Orange, 20=Mango} Class Constructors
The following is the class constructor supported by the Dictionary class in Java −
| Sr.No. | Constructor & Description |
|---|---|
| 1 | Dictionary() This is the single constructor. |
Class Methods
The following are the class methods supported by the Dictionary class in Java −
| Sr.No. | Method & Description |
|---|---|
| 1 | abstract Enumeration<V> elements() This method returns an enumeration of the values in this dictionary. |
| 2 | abstract V get(Object key) This method returns the value to which the key is mapped in this dictionary. |
| 3 | abstract boolean isEmpty() This method tests if this dictionary maps no keys to a value. |
| 4 | abstract Enumeration<K> keys() This method returns an enumeration of the keys in this dictionary. |
| 5 | abstract V put(K key, V value) This method maps the specified key to the specified value in this dictionary. |
| 6 | abstract V remove(Object key) This method removes the key (and its corresponding value) from this dictionary. |
| 7 | abstract int size() This method returns the number of entries (distinct keys) in this dictionary. |
Methods Inherited
The Dictionary class inherits methods from the following classes −
- java.util.Object
Adding and Iterating Mappings in the Dictionary
The Java Dictionary put(K, V) method is used to add a key-value pair. We're creating a dictionary instance using a Hashtable object of Integer, Integer. Then we've added a few elements to it using the put(K, V) method. An enumeration is retrieved using the elements() method, and the enumeration is then iterated to print the elements of the dictionary.
Example
Below is an example of adding pairs to a Dictionary and iterating over them using enumeration in Java −
package com.tutorialspoint; import java.util.Enumeration; import java.util.Dictionary; import java.util.Hashtable; public class DictionaryDemo { public static void main(String[] args) { // creating a dictionary with a hashtable Dictionary<Integer, Integer> dictionary = new Hashtable<>(); // add 2 pairs using the put() method dictionary.put(1, 1); dictionary.put(2, 2); // using the enumeration for iterating over the pairs Enumeration<Integer> enumeration = dictionary.elements(); while(enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } } } Output
Let us compile and run the above program. This will produce the following result −
2 1
Removing a Mapping from the Dictionary
The Java Dictionary remove(K) method is used to remove a key-value pair. We're creating a dictionary instance using a Hashtable object of Integer, Integer. Then we've added a few elements to it using the put(K, V) method. Then, using the remove() method, we have removed one mapping and printed the dictionary.
Example
Below is an example of removing a pair from a Dictionary in Java −
package com.tutorialspoint; import java.util.Dictionary; import java.util.Hashtable; public class DictionaryDemo { public static void main(String[] args) { // creating a dictionary with a hashtable Dictionary<Integer, Integer> dictionary = new Hashtable<>(); // add 3 pairs using the put() method dictionary.put(1, 1); dictionary.put(2, 2); dictionary.put(3, 3); // using the remove() method dictionary.remove(1); System.out.println(dictionary); } } Output
Let us compile and run the above program. This will produce the following result −
{3=3, 2=2}