http://www.hudatutorials.com

Download link: http://www.hudatutorials.com/ht/java/util/hashtable.html

java.util.Hashtable Class

A Hashtable is a specialized Dictionary that relies on a hashing algorithm to convert keys into a mechanism to look up values in the dictionary. The hashing algorithm provides a quick way to convert any object into something that can serve as a look−up mechanism. This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. Note that the hash table is open: in the case of a "hash collision", a single bucket stores multiple entries, which must be searched sequentially. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. The initial capacity and load factor parameters are merely hints to the implementation. The exact details as to when and whether the rehash method is invoked are implementation-dependent.

Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Hashtable operations, including get and put).

The initial capacity controls a tradeoff between wasted space and the need for rehash operations, which are time-consuming. No rehash operations will ever occur if the initial capacity is greater than the maximum number of entries the Hashtable will contain divided by its load factor. However, setting the initial capacity too high can waste space.

If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

/* java.util.Hashtable class Example */

/* Save with file name HashtableExample.java */

import java.util.Hashtable;

import java.util.Enumeration;

public class HashtableExample

{

public static void main(String args[])

{

//java.util.Hashtable DECLARATION

Hashtable<String,Integer> h;

//java.util.Hashtable OBJECT CREATION

//USING DEFAULT CONSTRUCTOR

h = new Hashtable<String,Integer>();

//ADD AN ELEMENTS

h.put("ONE", new Integer(1));

h.put("TWO", new Integer(2));

h.put("THREE", new Integer(3));

//Hashtable OUTPUT

Enumeration e = h.elements();

while(e.hasMoreElements())

{

System.out.println(e.nextElement());

}

}

}

/* java.util.Hashtable class Example 2 */

/* Save with file name HashtableExample2.java */

import java.util.Hashtable;

import java.util.Enumeration;

import java.util.Collection;

import java.util.Iterator;

public class HashtableExample2

{

public static void main(String args[])

{

//java.util.Hashtable DECLARATION

Hashtable<String,Integer> h;

//java.util.Hashtable OBJECT CREATION

//USING DEFAULT CONSTRUCTOR

h = new Hashtable<String,Integer>();

//ADD AN ELEMENTS

h.put("ONE", new Integer(1));

h.put("TWO", new Integer(2));

h.put("THREE", new Integer(3));

//Hashtable KEYS OUTPUT

Enumeration e = h.keys();

int i=1;

while(e.hasMoreElements())

{

System.out.println("Key " + i++ + " : " + e.nextElement());

}

//Hashtable VALUES OUTPUT

Collection c = h.values();

Iterator values = c.iterator();

int j=1;

while(values.hasNext())

{

System.out.println("Value " + j++ + " : " + values.next());

}

}

}

/* java.util.Hashtable class Example 3*/

/* Save with file name HashtableExample3.java */

import java.util.Hashtable;

import java.util.Enumeration;

public class HashtableExample3

{

public static void main(String args[])

{

//java.util.Hashtable DECLARATION

Hashtable<String,Integer> h;

//java.util.Hashtable OBJECT CREATION

//USING DEFAULT CONSTRUCTOR

h = new Hashtable<String,Integer>();

//ADD AN ELEMENTS

h.put("ONE", new Integer(1));

h.put("TWO", new Integer(2));

h.put("THREE", new Integer(3));

System.out.println("isEmpty : " + h.isEmpty());

//RETURNS THE NUMBER OF KEYS IN THIS Hashtable

System.out.println("noof Keys : " + h.size());

System.out.println("Key ONE value : " + h.get("ONE"));

System.out.println("Contains key THREE : " + h.containsKey("THREE"));

System.out.println("Contains value 2 : " + h.contains (new Integer(2)));

}

}

The following example shows how to save Hashtable into file.

/* java.util.Hashtable class Example 4 */

/* Save with file name HashtableExample4.java */

import java.util.Hashtable;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

public class HashtableExample4

{

public static void main(String args[])

{

try

{

//java.util.Hashtable DECLARATION

Hashtable<String,Integer> h;

//java.util.Hashtable OBJECT CREATION

//USING DEFAULT CONSTRUCTOR

h = new Hashtable<String,Integer>();

//ADD AN ELEMENTS

h.put("ONE", new Integer(1));

h.put("TWO", new Integer(2));

h.put("THREE", new Integer(3));

//FileOutputStream CREATION

FileOutputStream fos = new FileOutputStream("hashtable.set");

//ObjectOutputStream CREATION

ObjectOutputStream oos = new ObjectOutputStream(fos);

//WRITE Set OBJECT TO ObjectOutputStream

oos.writeObject(h);

//CLOSE THE ObjectOutputStream

oos.close();

System.out.println("HashTable Saved into File Sucessfully");

}

catch(Exception e)

{

System.out.println("Error Occurred : " + e.getMessage());

}

}

}

The following example shows how to retrieve Hashtable from file.

/* java.util.Hashtable class Example 5 */

/* Save with file name HashtableExample5.java */

import java.util.Hashtable;

import java.io.FileInputStream;

import java.io.ObjectInputStream;

public class HashtableExample5

{

public static void main(String args[])

{

try

{

//java.util.Hashtable DECLARATION

Hashtable<String,Integer> h;

//FileInputStream CREATION

FileInputStream fis = new FileInputStream("hashtable.set");

//ObjectInputStream CREATION

ObjectInputStream ois = new ObjectInputStream(fis);

//READ Hashtable OBJECT FROM ObjectInputStream

h = (Hashtable) ois.readObject();

ois.close();

System.out.println(h);

}

catch(Exception e)

{

System.out.println("Error Occurred : " + e.getMessage());

}

}

}

5