Generics Hashtable

Three programs are given on Hashtable.

  1. Hashtable General: 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 second program (of the three) that uses the general methods of addition, retrieval and printing the elements. The methods used are put(), isEmpty(), size(), keySet(), values(), putAll(), iterator() and get().

Example on Generics Hashtable
import java.util.*;
public class HashtableGenerics
{
  public static void main(String args[])
  {
    Hashtable ht1 = new Hashtable();
    System.out.println("isEmpty() before adding elements: " + ht1.isEmpty());

    ht1.put("ten", 10);
    ht1.put("eleven", 11);
    ht1.put("twelve", 12);
    ht1.put("thirteen", 13);
    ht1.put("fourteen", 14);
    System.out.println("isEmpty() after adding elements: " + ht1.isEmpty());
    System.out.println("No. of elements: " + ht1.size());

    System.out.print("Keys of ht1: ");
    Set mySet = ht1.keySet();
    Iterator it1 = mySet.iterator();
    while(it1.hasNext())
    {
      System.out.print(it1.next() + " ");
    }

    System.out.print("\nValues of ht1: ");
    Collection col = ht1.values();
    for(int k : col)
    {
      System.out.print(k + " ");
    }
   
    Hashtable ht2 = new Hashtable();
    ht2.putAll(ht1);
    ht2.put("fifteen", 15);

    System.out.println("\n\nKey/values of ht2: ");
    Set yourSet = ht2.keySet();
    Iterator it2 = yourSet.iterator();
    while(it2.hasNext())
    {
      Object k = it2.next();
      Object v = ht2.get(k);
      System.out.println(k + " : " + v);
    }

    System.out.println("\nht2 size before remove(): " + ht2.size());

    ht2.remove("ten");
    System.out.println("ht2 size after remove(): " + ht2.size());

    ht2.clear();
    System.out.println("\nht2 size after clear(): " + ht2.size());
  }
}


Generics Hashtable
Output screenshot on Generics Hashtable

Hashtable ht1 = new Hashtable();
System.out.println("isEmpty() before adding elements: " + ht1.isEmpty());

A generics Hashtable object ht1 is created that stores strings as keys and integers as values and is checked for its emptiness. isEmpty() method when called on a Hashtable object returns a boolean value of true if no elements exists. In the above statement, it prints true as no elements are added still.

ht1.put("ten", 10);
System.out.println("No. of elements: " + ht1.size());

With put() method, key/value pairs are added to the hash table. As per the generics, the first parameter is a string and the second parameter is an integer. The size() method returns an integer value stating the number of elements (key/value pairs) exist with the Hashtable object.

Four styles of retrieving the values are shown.

1. With Enumeration using keys() method. This is illustrated in the first program – HashtableGeneral.

2. With keys using keySet() method.

       Set mySet = ht1.keySet();
       Iterator it1 = mySet.iterator();
       while(it1.hasNext())
       {
         System.out.print(it1.next() + " ");
       }

The keySet() method returns an object of Set interface that contains all the keys of the hash table. The designers preferred Set to return with a designing principle. The set does not accept duplicates and also hash table; both stores unique elements. For this reason, the keySet() method returns a Set object. The iterator() method of Set returns an object of Iterator. The next() method returns the key and not the value. To get the values, the above loop can be modified as follows.

       while(it1.hasNext())
       {
         Object key = it1.next();
         Object value = ht1.get(key);
         System.out.println(key + " : " + value);
      }

3. With values using values() method.

       Collection col = ht1.values();
       for(int k : col)
       {
         System.out.print(k + " ");
       }

The values() method of Hashtable returns an object of Collection interface. What is the designing principle here? We know, values in a hash table may be in duplicates. Collection also can have duplicates. For this, the designers preferred to return Collection object with values() method. With enhanced for loop, the values are printed.

4. Using basic (general) for loop

       Collection col = ht1.values();
       ArrayList al1 = new ArrayList(col);
       for(int i = 0; i < ht1.size(); i++)
       {
         System.out.print(al1.get(i) + " ");
       }

Collection object is passed to ArrayList constructor and converted the Collection into ArrayList. With size() and get() methods of ArrayList, the values are printed.

       Hashtable ht2 = new Hashtable();
       ht2.putAll(ht1);
       ht2.put("fifteen", 15);

Another Hashtable object ht2 is created and all the elements (key/value pairs) of ht1 are added at a time to ht2. With put() method, one more element is also added. The elements of ht2 are printed with iterator explained in the previous 2nd style.

   
       System.out.println("\nht2 size before remove(): " + ht2.size());

The size() method returns the number of elements stored in the hash table.

       ht2.remove("ten");

The remove() method deletes the element from hash table. When removed, the size lessened by 1.

       ht2.clear();
       System.out.println("\nht2 size after clear(): " + ht2.size());

The clear() method deletes all the elements. Now the size() method prints 0.

Leave a Comment

Your email address will not be published.