Hashtable in java

Hashtable was part of the original java.util and is a concrete implementation of a Dictionary. However, with the advent of collections, Hashtable was reengineered to also implement the Map interface. Thus, Hashtable is now integrated into the Collections Framework. It is similar to HashMap, but is synchronized

Hashtable was made generic by JDK 5. It is declared like this:

class Hashtable<K, V>

Here, specifies the type of keys, and specifies the type of values.

The Hashtable constructors are shown here:

Hashtable( )

Hashtable(int size)

Hashtable(int size, float fillRatio)

Hashtable(Map<? extends K, ? extends V> m)

 The Legacy Methods Defined by Hashtable

MethodDescription 
void clear( )Resets and empties the hash table. 
   
Object clone( )Returns a duplicate of the invoking object. 
   
boolean contains(Object value)Returns true if some value equal to value exists within the hash table. 
 Returns false if the value isn’t found. 
   
boolean containsKey(Object key)Returns true if some key equal to key exists within the hash table. 
 Returns false if the key isn’t found. 
   
boolean containsValue(Object value)Returns true if some value equal to value exists within the hash table. 
 Returns false if the value isn’t found. 
   
Enumeration<V> elements( )Returns an enumeration of the values contained in the hash table. 
   
V get(Object key)Returns the object that contains the value associated with key. 
 If key is not in the hash table, a null object is returned. 
   
boolean isEmpty( )Returns true if the hash table is empty; returns false if it contains 
 at least one key. 
   
Enumeration<K> keys( )Returns an enumeration of the keys contained in the hash table. 
   
V put(K key, V value)Inserts a key and a value into the hash table. Returns null if key isn’t 
 already in the hash table; returns the previous value associated with 
 key if key is already in the hash table. 
   
void rehash( )Increases the size of the hash table and rehashes all of its keys. 
   
V remove(Object key)Removes key and its value. Returns the value associated with key. 
 If key is not in the hash table, a null object is returned. 
   
int size( )Returns the number of entries in the hash table. 
   
String toString( )Returns the string equivalent of a hash table. 

Demonstrate a Hashtable

import java.util.*;

class HTDemo {

public static void main(String args[]) { Hashtable<String, Double> balance =

new Hashtable<String, Double>();

Enumeration<String> names; String str;

double bal;

balance.put("John Doe", 3434.34); balance.put("Tom Smith", 123.22); balance.put("Jane Baker", 1378.00); balance.put("Tod Hall", 99.22); balance.put("Ralph Smith", -19.08);

//Show all balances in hashtable. names = balance.keys();
while(names.hasMoreElements()) { str = names.nextElement(); System.out.println(str + ": " +

balance.get(str));

}

System.out.println();

//Deposit 1,000 into John Doe's account. bal = balance.get("John Doe"); balance.put("John Doe", bal+1000); System.out.println("John Doe's new balance: " +

balance.get("John Doe"));

}

}

The output from this program is shown here:

Todd Hall: 99.22

Ralph Smith: -19.08

John Doe: 3434.34

Jane Baker: 1378.0

Tom Smith: 123.22

John Doe’s new balance: 4434.34

Leave a Comment