Java

Java Dictionary

The Java dictionary is part of the java.util package, also known as the child class of the Hashtable class. The Dictionary in Java associates keys with values, just as the Hashtable class. The Java Dictionary class contains key-value pairs similar to the Map interface. The key and value are the instances; any non-null instance can serve both as a key and a value. The Java Dictionary class is listed with various methods which provide different functionality for the dictionary.

Example 1:

The dictionary class – put() method – of Java is used to add the mapping dictionary. This indicates that a given dictionary may map a specific key and value.

Here, we employ a Java class “putMethodCase” which enclosed the main() method definition. The main() method is further implemented with the source code. We first create an empty dictionary inside the “MyDict” object. The empty dictionary is specified with the Hashtable map interface which accepts the key as an integer and the value against the key as a string. Next, we invoke the put() method with the “MyDict” dictionary object and insert the elements in the form of key and value pairs. After that, we display the dictionary which contains the key and value elements. We call the put() method again to add the new value against the existing key. Then, display the modified dictionary.

The empty dictionary filled with the put() method is demonstrated on the following screen. Moreover, the modified value of the existing key is also shown:

Example 2:

The Java Dictionary’s get() method can also be used to acquire the value of the provided key in the dictionary. The value of the key in the given dictionary is returned by the get() method.

Here, we first construct the Java class “getMethodCase” and deploy the main() method for the source code of the Java Dictionary. A Java dictionary class object named “D1” is created in the main() method. Initially, the dictionary is empty and only accepts the key and value in the string data type. Then, to insert the mapped values in the “D1” dictionary, we call the put() method. The keys are specified with the string number format and the values contain the strings. After that, we have a print class where the key is specified in the get() method from the “D1” dictionary.

The number of the key “5” that is present in the given dictionary is retrieved from the get() method.

Example 3:

The Java Dictionary class provides the IsEmpty() method to find out whether the dictionary contains the key-value pairs or not. The IsEmpty has a Boolean return type. If this dictionary is empty, it gives true; otherwise, it provides false.

Here is a Java “IsEmptyMethodCase” class which is established with the main() method. The main() method is further defined with the main program. We first initialize an empty dictionary which is declared in the “ColorDict” object. The dictionary is extended from the Hashtable class and the put() method is used to insert the mapped values in that dictionary. The mapped value follows the <Integer, String> format. We print the dictionary with the mapped values on the screen. After that, we decalre a “Boolean” object “Check” where the IsEmpty() method is deployed with the “ColorDict” dictionary object. The IsEmpty() verifies if the specified dictionary is empty or has a mapped value which is displayed at compilation time.

Since the mapped values are inserted into the dictionary via the put() method, the IsEmpty() method returns true on the following terminal screen:

Example 4:

To obtain the entire list of keys in the dictionary, another technique is available in the Java Dictionary. The size() method is the method that returns the total keys in the dictionary and it requires no parameters.

Here, we create the Java class “SizeMethodCase” to get the size of the dictionary. For this, we employ the main() method because Java requires the main method for the implementation. We instantiate the “StudentMarks” Hashtable object to create the dictionary. First, we have an empty dictionary. Then, the elements are inserted as (K, V) map inside the dictionary with the put() method. We have the print command to display the size of the dictionary by calling the size() method inside it with the “StudentMarks” object which has the dictionary. After that, we add two more mapped values in the existing dictionary and fetch the number of the keys that the dictionary holds from the size() method.

The first dictionary which is displayed in the following illustration has three keys; the second is the previous dictionary with the modified size.

Example 5:

Now, the Java Dictionary class has the elements() and keys() methods to get the enumeration from the dictionary. The elements() method gives the enumeration of the entire values of the keys. The keys() method is used to return the enumeration of all the keys from the dictionary.

Here, we have an “ElementAndKeyMethodCase” class where the main() method is called with the program implementation. We create the “DictValue” object for the dictionary which is extended by the Hashtable class. Then, we insert the keys and values inside the dictionary by employing the put() method. Next, we have a for loop to iterate over each value from the dictionary. We call the elements() method inside the for loop from the enumeration object “enum” and display all the values of the dictionary. In the same way, we iterate the keys of the dictionary and get all the keys from the keys() method which is also printed on the output screen.

The elements() method gets the enumeration values from the following dictionary. Then, the keys() method outputs all the keys enumeration of the dictionary.

Conclusion

Java Dictionary is the abstract class that contains the keys with the value. Hashtable inherits Dictionary in the Java hierarchy and defines the Map. Every key in a single Dictionary object has a maximum of one value associated with it. Java Dictionary employs classes and procedures from before the Collections Framework. We explored several aspects of Java Dictionary which includes adding a key and value in the dictionary and getting the specific key and size of the dictionary.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.