Java2all.com  

Hashtable





Share This Topic -

Hashtable :

Hashtable is a part of the java.util library and is a concrete implementation of a dictionary.

(Dictionary is a class that represents a key/value storage repository. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key.)

Hashtable stores key/value pairs in a hash table.

When using a Hashtable, you specify an object that is used as a key, and the value that you want to link to that key.

The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

The Hashtable constructors are shown here:

            Hashtable()

            Hashtable(int size)

The first constructor is the default constructor.

The second constructor creates a hash table that has an initial size specified by size.

The methods available with Hashtable are

Method

Description

Void clear()

Resets and empties the hash table.

Boolean containsKey(Object key)

Returns true if some key equals 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 elements( )

Returns an enumeration of the values contained in the hash table.

Object 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 keys( )

Returns an enumeration of the keys contained in the hash table.

Object put(Object key Object value)

Inserts a key and a value into the hash table.

Object 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.

EX :

 import java.util.*;

 public class hash1 
{
	public static void main(String args[]) 
	{
		Hashtable marks = new Hashtable();
		Enumeration names;
		Enumeration emarks;
		String str;
		int nm;
		
		// Checks wheather the hashtable is empty
		System.out.println("Is Hashtable empty " + marks.isEmpty());
		marks.put("Ram", 58);
		marks.put("Laxman", 88);
		marks.put("Bharat", 69);
		marks.put("Krishna", 99);
		marks.put("Janki", 54);
		
		System.out.println("Is Hashtable empty " + marks.isEmpty());
		// Creates enumeration of keys
		names = marks.keys();
		while(names.hasMoreElements()) 
		{
			str = (String) names.nextElement();
			System.out.println(str + ": " +	marks.get(str));
		}
		/*
		nm = (Integer) marks.get("Janki");
		marks.put("Janki", nm+15);
		System.out.println("Janki's new marks: " +	marks.get("Janki"));
		
		// Creates enumeration of values
		emarks = marks.elements();
		while(emarks.hasMoreElements()) 
		{
			nm = (Integer) emarks.nextElement();
			System.out.println(nm);
		}
		
		// Number of entries in a hashtable
		System.out.println("The number of entries in a table are " + marks.size());
		
		// Checking wheather the element available
		
		System.out.println("The element is their " + marks.containsValue(88));
		*/
		// Removing an element from hashtable
		
		System.out.println("========");
		marks.remove("Bharat");
		names = marks.keys();
		while(names.hasMoreElements()) 
		{
			str = (String) names.nextElement();
			System.out.println(str + ": " +	marks.get(str));
		}
		// Returning an String equivalent of the Hashtable
		
		System.out.println("String " + marks.toString());
		// Emptying hashtable
		
		marks.clear();
		System.out.println("Is Hashtable empty " + marks.isEmpty());
	}
}

 
Output :
 
Laxman: 88
Janki: 54
Ram: 58
String {Krishna=99, Laxman=88, Janki=54, Ram=58}
Is Hashtable empty true