Generics Hashtable

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


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.

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.

3. With values using values() method.

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 object is passed to ArrayList constructor and converted the Collection into ArrayList. With size() and get() methods of ArrayList, the values are printed.

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.

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *