Java Hashtable Program


Java Hashtable Program

Summary: By the end of this tutorial "Java Hashtable Program", you will comfortable to write a program. Explained in simple terms with screenshot.

Three Java Hashtable Programs are given.
  1. HashtableGeneral: Uses the methods like elements, containsKey, put, get, contains and keys.
  2. Hashtable Generics: Creates generics Hashtable storing strings and integers and uses the methods like keySet, values, hashCode, putAll, clear, size, iterator, remove and isempty etc.
  3. Hashtable Special: Uses the methods like clone(), hashCode() and the values are printed in the ascending order of keys.

It is advised to read Hashtable About before going through this program to get acquainted with the properties, constructors and methods of Hashtable.

This is the first Java Hashtable Program (of the three) that uses the general methods of addition, retrieval and printing the elements. The methods used are put(), get(), containsKey(), contains() and keys(),

import java.util.*;
public class HashtableGeneral
{
  public static void main(String args[])
  {
    Hashtable ht = new Hashtable();

    ht.put("apple", "red");
    ht.put(10, 20);
    ht.put("birthday", new Date());   
    ht.put("interest", 200.5);

    System.out.println("apple value before overriding: " + ht.get("apple"));

    ht.put("apple", "green");    
    System.out.println("apple value after overriding: " + ht.get("apple"));

    System.out.println("Key birthday exists: " + ht.containsKey("birthday"));
    System.out.println("Value 200.5 exists: " + ht.contains(200.5));

    System.out.println("\nPrinting keys and values with Enumeration:");
    Enumeration e = ht.keys();
    while(e.hasMoreElements())
    {
      Object k = e.nextElement();
      Object v = ht.get(k);
      System.out.println(k + " : " + v );
    } 
   }
}


Java Hashtable Program
Output screen of Java Hashtable Program HashtableGeneral.java

Hashtable ht = new Hashtable();
ht.put("apple", "red");

A Hashtable object ht is created using the default constructor and a few elements (key/value pairs) like apple, birthday etc. are added with put() method.

System.out.println("apple value before overriding: " + ht.get("apple"));

The get() method returns the value associated with "apple”". It is "red".

ht.put("apple", "green");
System.out.println(“apple value after overriding: ” + ht.get("apple"));

The "apple" is added again with value green. As hash table cannot have duplicate keys, it overrides the earlier value red with green. Now the above get("apple") returns "green".

System.out.println("Key birthday exists: " + ht.containsKey("birthday"));
System.out.println("Value 200.5 exists: " + ht.contains(200.5));

containsKey("birthday") method checks the existence of "birthday" in hash table ht. As it exists, it returns true. Similarly contains(200.5) checks the availability of value 200.5 in the hash table ht. As it is available it returns true.

Enumeration e = ht.keys();

The method keys() returns an Enumeration object (like elements() method of Vector) containing all the keys of hash table. Using hasMoreElements() and next() methods, all the key/value pairs are printed.

Hashtable methods are thread safe as methods are synchronized like that of Vector.

Leave a Comment

Your email address will not be published.