Dictionary Class in JavaIn Java, Dictionary is the list of key-value pairs. We can store, retrieve, remove, get, and put values in the dictionary by using the Java Dictionary class. In this section, we will discuss the Java Dictionary class that stores data in key-value pairs just like the Map interface. Java Dictionary ClassJava Dictionary class is an abstract class parent class of any class. It belongs to java.util package. Its direct known subclass is the Hashtable class. Like the Hashtable class, it also maps the keys to values. Note that every key and value is an object and any non-null object can be used as a key and as a value. The Dictionary class hierarchy is as follows: Every key is associated with at most one value, as shown in the following figure. Once the value is stored in a dictionary object, we can retrieve it by using the key. Syntax: Note: The class is obsolete. So, implement the map interface instead of extending the class.Dictionary Class ConstructorThe class has only a constructor called a sole constructor. Syntax: Dictionary Class MethodsAll the methods of the Dictionary class are abstract. The following table describes the methods.
Java Dictionary ProgramsUse of Dictionary.put() MethodThe put() method inserts the elements in the dictionary. The following program demonstrates the same. InsertElementExample.java Output: {108=Canberra, 107=Nelson Bay, 106=Mount Gambier, 105=Lismore, 104=Perth, 103=Melbourne, 102=Brisbane, 101=Sydney} Use of Dictionary.size() MethodThe size of the dictionary is the number of elements the dictionary contains. In the following program, the size of the dictionary is 6. DictionarySizeExample.java Output: The size of the dictionary is: 6 Use of Dictionary.get() MethodBy using the get() method, we can retrieve the value of a specified key. DictionaryGetElement.java Output: The value of the specified key is: Melbourne Use of Dictionary.isEmpty() MethodIt returns true if dictionary is empty, else returns false. EmptyCheckExample.java Output: Is the dictionary empty? false Use of Dictionary.remove() MethodThe method removes the key and corresponding value that we have parsed in the method. The removed value is returned by the method. RemoveElementExample.java Output: The removed value is: Mount Gambier Use of elements() and key() MethodsRemoveElementExample.java Output: Dictionary values are: Canberra Nelson Bay Mount Gambier Lismore Perth Melbourne Brisbane Sydney Dictionary keys are: 108 107 106 105 104 103 102 101 Difference Between the HashMap and Dictionary ClassThe Java HashMap class and the Dictionary class both perform a similar function. The only difference is that HashMap implements the Map Interface while the Dictionary class does not. According to Java documentation, the Dictionary class is no longer in use because it is outdated. Instead of the Dictionary class, the HashMap class is used. Since we can say that HashMap is a type of dictionary. Next TopicLeft View of a Binary Tree in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India