Java collections Framework- Hashtable Class

Java collections Framework- Hashtable Class


Posted in : Java Posted on : December 11, 2010 at 6:15 PM Comments : [ 0 ]

Hashtable class is part of java.util package. Hashtable class can add key and value put(key, value) pair elements.

Java collections Framework- Hashtable Class

Hashtable class is part of java.util package. Hashtable class can add key and value put(key, value) pair elements. This Hashtable permits null key and value. Hashtable is synchronized, but is synchronized. This class implements a hashtable, which maps keys to values. Any non-null 
object can be used as a key or as a value.
Like HashMap, 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 linked 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 class supports four constructors.

Hashtable( ) - This constructor constructs an empty Hashtable with the default initial capacity (11) and the default load factor (0.75).

Hashtable(int initialCapacity) - This constructor constructs an empty Hashtable with the specified initial capacity.

Hashtable(int initialCapacity, float loadFactor) - This constructor constructs an empty Hashtable with the specified initial capacity and load factor.

Hashtable(Map m) - This constructor constructs a new Hashtable with the same mappings as the specified Map.

The list of methods supported by Hashtable Class are shown given below:

put( ) -          Associates the specified value with the specified key in this Hashtable. 

clear( ) -       Removes all mappings from the Hashtable.

isEmpty( ) -  Returns true if this Hashtable contains no key-value mappings. 

remove( ) -   Removes the mapping for this key from this Hashtable if it is present. 

keySet( ) -    Returns a set view of the keys contained in this map. 

entrySet( ) - Returns a collection view of the mappings contained in this map. 

values( ) -     Returns a collection view of the values contained in this map. 

size( ) -         Returns the number of key-value mappings in this Hashtable.

Here is the  example for demonstration of the Hashtable -

Example:- HashTableDemo.java

package devmanuals.com;

import java.util.*;

public class HashtableDemo {
	public static void main(String args[]) {
		Hashtable Htable = new Hashtable();
		Htable.put(1, "Singh");
		Htable.put(3, "Kumar");
		Htable.put(4, "Kumar");
		Htable.put(2, "Nagar");
		Htable.put(6, "Prakash");
		System.out.println("The elements of Hashtable is : " + Htable);

	}
}
Output:-
The elements of Hashtable is : {6=Prakash, 4=Kumar, 3=Kumar, 2=Nagar, 1=Singh}

Download This Code.

Here is the Example of methods implementation of Hashtable class.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics