Java

How to Create a Dictionary in Java

While managing the data in Java, there can be instances where the developer wants to accumulate the data in a sorted manner. For instance, appending the associated entries against one another in the form of “key-value” pairs. In such situations, creating a “dictionary” in Java assists in streamlining the data access and enhancing the code readability at the programmer’s end.

This article will elaborate on the approaches to creating and performing various operations upon a “dictionary” in Java.

How to Create a Dictionary in Java?

A “Dictionary” in Java can be created using the following approaches:

Approach 1: Create a Dictionary in Java Using “Hashtable” Object

The “HashTable.put()” method is used to insert the key-value pairs in the “HashTable”. The “HashTable.get()” method is utilized to fetch the value to which the specified key is mapped in the hashtable. The “isEmpty()” method checks if the associated Hashtable object is empty. These methods can be applied in combination to insert and retrieve the “key-value” pairs and check for the empty Hashtable.

Syntax

public V put(key, val)

 
In this syntax:

    • key” represents the hashtable key.
    • val” corresponds to the hashtable value.

 

public V get(Obj key)

 
In the given syntax, the “Obj key” refers to the hashtable key.

Before proceeding to the code, make sure to include the following package to access all the classes and methods:

import java.util.*;

 
Now, move on to the following code:

Hashtable<Integer, String> dictionary = new Hashtable<Integer, String>();
 dictionary.put(1, "Harry");
 dictionary.put(2, "David");
 dictionary.put(3, "Tim");
System.out.println("The dictionary elements are: "+dictionary);
System.out.println("The string value at key 1 is: " + dictionary.get(1));
if (dictionary.get(3)!=null) {
 dictionary.remove(3);
System.out.println("The value removed!"); }
System.out.println("Is the dictionary empty? : " + dictionary.isEmpty());
System.out.println("The dictionary elements are: "+ dictionary);

 
In the above code snippet, apply the following steps:

    • Firstly, create a dictionary with the help of the “Hashtable” object named “dictionary” via the “new” keyword and the “Hashtable()” constructor, respectively.
    • Note: The “<Integer, String>” indicates that the supported data type for the key is “Integer” and for the value is “String”.
    • In the next step, insert the stated values in the form of “key-value” pairs in accordance with the specified data types.
    • After that, fetch the value against the specified key via the associated “get()” method.
    • In the “if” statement, apply a check upon the key “3” such that it becomes omitted upon being “not null” via the “remove()” method.
    • Lastly, check for the empty dictionary using the “isEmpty()” method and log the updated dictionary elements in the form of “key-value” pairs.

Output


In the above output, it can be analyzed that both the integer keys and the string values are inserted and fetched appropriately.

Approach 2: Create a Dictionary in Java Using “HashMap” Object

The “containsKey()” method checks if the mapping for the specified key is contained in the hashmap and the “containsValue()” method is utilized to verify if the specified value is accumulated in the hashmap. These approaches can be applied combined with the discussed methods to insert, fetch, or search for a particular “key” or “value” in the hashmap.

Syntax

hashmap.containsKey(Obj key)

 
In this syntax, the specified “Obj key” refers to the key that needs to be searched in the hashmap.

hashmap.containsValue(Obj value)

 
In the above syntax, the “Obj value” corresponds to the contained value in the associated hashmap.

Before heading to the code functionalities, include the following libraries to work with “Map” and “HashMap”, respectively:

import java.util.Map;
import java.util.HashMap;

 
Now, let’s move on to the below-provided lines of code:

Map<Integer, Object> dictionary = new HashMap<Integer, Object>();
dictionary.put(1, "Harry");
dictionary.put(2, 24);
dictionary.put(3, "Tim");
System.out.println("The dictionary elements are: "+dictionary);
System.out.println("The object value at key 1 is: " + dictionary.get(1));
System.out.println("Does the dictionary contain the key 3?: " +
dictionary.containsKey(3));
System.out.println("Does the dictionary contain the value John? : "
+ dictionary.containsValue("John"));
System.out.println("The dictionary size is: " + dictionary.size());

 
In this code, perform the following steps:

    • First of all, create a dictionary using the “HashMap” object and recall the discussed approach for specifying the data type of the key-value pairs.
    • Note: The “Object” type can accumulate both integer and string values.
    • In the next step, likewise, associate the “put()” and “get()” methods to insert/add and retrieve the key-value pairs, respectively.
    • After that, apply the “containsKey()” and “containsValue()” methods to check for the specified key and value in the “HashMap”.
    • Finally, log the HashMap size on the console via the associated “size()” method.

Output


In this outcome, it can be implied that the dictionary is implemented via the “HashMap”.

Conclusion

A Dictionary in Java can be created using the “Hashtable” or “HashMap” objects and various operations can be performed by utilizing the class methods. These methods assist in inserting, fetching, accessing, or deleting the object elements, etc. This blog discussed the approaches to creating a dictionary in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.