Understanding the Java Hashtable Collection

Java hashtableA software application needs to store data. This data can be a single unit or in the form of collections. For instance, an application might need to store information at start time such as a single unit of data and can be stored in date type variable. However, when it comes to a collection of data, such as records of all the students enrolled in a particular course, a collection type is required. Java contains several data types that can be used to store collections of data. These types vary in complexity ranging from a simple array type to a complex Dictionary, Hashmap or TreeMap collection types. One such collection type is the Java Hashstable.

What is Java Hashtable?

Java Hashtable is a class that belongs to collection framework of Java library. Initially, the Hashtable class belonged to java.util namespace and was implemented as concrete form of the Dictionary class. But with many transformations that Java 2 brought, the Hashtable class was also modified to implement the Map interface and was placed into collection framework.

Interested in learning more about Java? Take a course at Udemy.com.

How Java Hashtable works?

The Java Hashtable class is similar in functionality to HashMap. The Hashtable class stores collection of key-value pairs. The first parameter is the key that can be any object and the second parameter is value which is also an object. A hashing algorithm calculates the hash index from the key and that hash index is used to refer to the value. A Hashtable is considered a synchronized form of HashMap class.

Instantiating and Adding elements to Hashtable Class

Instantiating a Java Hashtable class is a straight forward process. Like other classes in Java, a new instance of the Hashtable class in Java is constructed using a constructor. To add elements to a Hashtable object, the put method is called on the object of the Hashtable. The put method takes two object type parameters, where first parameter refers to key and the second parameter refers to value. Following example demonstrates how a Hashtable class can be instantiated via default constructor and how elements are added to the object of the Hashtable constructor via put method. But before executing this code, include java.util.Hashtable in the file containing the code:

import java.util.Hashtable;

public class MyClass {

public static void main(String[] args) {

Hashtable capitals = new Hashtable();

capitals.put("USA", "Washington");

capitals.put("England", "London");

capitals.put("France", "Paris");

capitals.put("China", "Beijing");

}

}

In the above code an object of Hashtable class, named capitals has been instantiated via the Hashtable’s default constructor. This Hashtable collection would store countries names as keys and the corresponding capitals as values. The put method has been called on capitals object and string type key-value pairs have been inserted into the table.

To learn more Java essentials, look at Udemy.com courses.

Traversing Hashtable Keys and Values

A Hashtable class contains a keys () method that returns all the keys in a Hashtable. The elements of the returned enumerations can be traversed by using hasMoreElements() method of the returned enumeration object. This method returns true until there are more elements in the enumeration. This method can be called inside the while loop.

Inside the while loop, nextElement() method can be called on the enumeration object. This method would return the current element to which enumeration is referring based on the hasMoreElements() method’s pointer which points to the next element whenever it is called. The following example demonstrates that how all the keys in the capitals collection, instantiated in the last example, can be traversed.

Enumeration country;

country = capitals.keys();

while (country.hasMoreElements())

{

String conname = (String) country.nextElement();

System.out.println(conname);

}

The above code displays all the keys in the Hashtable capitals collection instantiated in the last example. The output would be:

China

France

USA

England

 

To get all the values in the Hashtable, value has to be referenced via key. This is done by calling get method on Hashtable class instance. The get method takes a key as a parameter and returns the associated value with it. For traversing all the values in the capitals collection, the code in the last example can be extended as follows:

Enumeration country;

country = capitals.keys();

while (country.hasMoreElements())

{

String conname = (String) country.nextElement();

System.out.println("Key:"+conname + "      Value: "+capitals.get(conname));

}

The above snippet is similar to the one in the last example with only exception that the keys obtained via nextElement are passed as parameter to the capitals collection’s get method which returns the corresponding value associated with it. The output of the above code would be:

Key:China      Value: Beijing

Key:France      Value: Paris

Key:USA      Value: Washington

Key:England      Value: London

 

The contains and containsKey methods

In order to find if a Hashtable contains a particular key or a particular value, the contains and containsKey methods can be used respectively. The following example demonstrates the usage of  both of these methods:

Hashtable capitals = new Hashtable();

capitals.put("USA", "Washington");

capitals.put("England", "London");

capitals.put("France", "Paris");

capitals.put("China", "Beijing");

System.out.println(capitals.containsKey("England"));

System.out.println(capitals.contains("Paris"));

Here, the ContainsKey method has been called on capitals collection and a string value “England” has been passed to it, since capitals collection contains a key “England.” Therefore, this method would return true, which would be displayed on the console screen.

Similarly, the “contains” method takes a value and check if value exists in the collection. It returns true if it does. Since, value “Paris” exists in the collection capitals, the contains method would also return true like the containsKey method.

Clear and IsEmpty Methods

The clear method is used to clear the Hashtable, it removes all the key-value pairs from the Hashtable. The isEmpty method returns true if Hashtable is empty, otherwise this method returns false. The following example demonstrates the usage of these methods:

Hashtable capitals = new Hashtable();

capitals.put("USA", "Washington");

capitals.put("England", "London");

capitals.put("France", "Paris");

capitals.put("China", "Beijing");

System.out.println(capitals.isEmpty());

capitals.clear();

System.out.println(capitals.isEmpty());

Here, the isEmpty method is called on capitals collection after putting elements in the collection; this method would initially returns false. Next, the clear method has been called on the capitals collection which would clear the collection. Now, if the isEmpty method is again called on capitals collection, it would return true since the capital collection would now contain nothing.

For more Java collections tutorial, look at the available courses at Udemy.com.