Understanding the Java Dictionary Class

shutterstock_148972376Storing and collection of data is a routine task for a software application. Be it a data-centric application that stores records of students enrolled in a particular course or a weather update mobile application, every application somehow needs to store a collection of data. It’s for this reason that many programming languages provide built-in classes for storing these collections. The .NET framework provides collections and generic collection classes for storing data. Similarly, Java also contains several classes that can store data in the form of collections; these classes include arrays, lists, and dictionaries. This article presents a brief introduction to the Java Dictionary class that stores collections in the form of key and value pairs.

For more detailed Java tutorials, check out a course at Udemy.com.

What is a Java Dictionary Class?

The Java Dictionary class is an abstract class that allows all its derived classes to store data in the form of key-value pairs. Both keys and values can be objects of any type, but keys should be unique. Also, both keys and values cannot be null. For instance, if a key or value is referring to null and then inserted into a dictionary, a NullPointerException error occurs. Therefore, it’s mandatory to provide a non-null value for storing both keys and values.

How is the Java Dictionary Class Implemented?

The Java Dictionary class has now become obsolete and currently all the collection types that store data in the form of key-value pairs implement the Java Map interface. However, the Java Dictionary class variables can still be used to store a reference to the Hashtable classes implementing the Map interface. The three major classes that implement the Map interface and stores data in the form of key-value pair are as follows:

  1. Hashtable
  2. HashMap
  3. LinkedHashMap

Java Dictionary Examples

This section contains brief examples of Hashtable, HashMap and LinkedHashMap classes that store values in the form of key-value pairs similar to a dictionary collection. The Hashtable object is stored in the Dictionary type variable since it inherits directly from the Dictionary. But as previously stated, the Dictionary class has been deprecated in Java and now collections that store data in the form of key-value pair implement the new Map interface; therefore, the HashMap and LinkedHashMap objects would be stored in the Map type variable as seen in the following examples. The first example demonstrates the Hashtable implementation of the Dictionary class. The code is demonstrated below:

Hashtable:

Dictionary  cities = new Hashtable();

cities.put("New York", "USA");

cities.put("Toronto", "Canada");

cities.put("Manchester", "UK");

cities.put("Berlin", "Germany");

for (Enumeration city = cities.keys(); city.hasMoreElements();) {

String key = (String)city.nextElement();

System.out.println("Key: "+key +" Value: " + cities.get(key)); }

In the above example, a Dictionary type variable, named cities has been instantiated by storing a Hashtable type object in it. In order to store data to the Hashtable, the “put” method is used. This method takes two parameters. The first parameter is the key that can be of any type and the second parameter is the value associated with the key. The value can also be of any type.

A point worth remembering here is that keys must be unique. For instance, in the cities hashtable, keys are referring to the name of cities and corresponding values are the countries in which these cities are present. If a key “Newyork” is inserted once more in the cities hashtable, no error would be generated. However, if the cities collection is traversed, the key “New York” would appear only once, which means that repeated keys are ignored by the hashtable.

In order to get all the keys, the key method is called on the instance of Hashtable which returns all the keys in the form of enumeration. Next, the nextElement method can be called on the returned enumeration which returns true until all the elements in the enumeration have been traversed.

To get the value associated with each key, a get method is called on the instance of the Hashtable and the key is passed as a parameter to that get method. This returns the value associated with the specified key. The output of the aforementioned code would be:

Key: Manchester Value: UK

Key: Newyork Value: USA

Key: Toronto Value: Canada

Key: Berlin Value: Germany

 

New to Java and its essentials? Take a look at a Udemy.com course.

HashMap

HashMap is similar in its functionality to the Hashtable as both of them store collections of data in the form of key-value pairs. The difference between HashMap and Hashtable collection is that Hashtable implements  both the Dictionary and Map interface as well. The HashMap class only implements the Map interface and does not directly inherit from the Dictionary class. The following example demonstrates how a HashMap can be used to store a key-value pair with the same keys and values that were stored in the Hashtable in the last example:

Map  cities = new HashMap();

cities.put("Newyork", "USA");

cities.put("Toronto", "Canada");

cities.put("Manchester", "UK");

cities.put("Berlin", "Germany");

Set citykeys = cities.keySet();

for (Iterator city = citykeys.iterator(); city.hasNext();) {

String key = (String)city.next();

System.out.println("Key: "+key +" Value: "+cities.get(key));

}

In the above code, the HashMap class is instantiated and the reference of the object is stored in the Map type variable named cities. The process of inserting elements in the HashMap is similar to Hashtable using the put method. However, the process of traversing the keys is a little different.

Unlike Hashtable where the keys method returned an enumeration containing all the keys in a particular collection, the HashMap uses the keyset() method that returns a Set. The Iterator can then be used to traverse through this Set by calling the hasNext method on the Iterator. And to get the next key in the Set of keys, the next method can be called on the Iterator. The returned value is parsed to the string data type and displayed as output on the console.

To get the elements associated with a particular key, the get method can be used. Like, Hashtable, this method accepts a key as a parameter and returns a value associated with that key. The output of the above code would be similar to the Hashtable example.

The implementation for LinkedHashMap is similar except, replace the constructor of HashMap with LinkedHashMap in the last example. The output would also be similar.

Take a look at Udemy.com courses to learn more programming essentials.