Home Tutorial Java Core Java Collection : Hashtable

 
 

Java Collection : Hashtable
Posted on: October 9, 2012 at 12:00 AM
Advertisement
In this tutorial, we are going to discuss one of concept (Hashtable ) of Collection framework.

Java Collection : Hashtable

In this tutorial, we are going to discuss one of concept (Hashtable ) of Collection framework.

Hashtable  :

Hashtable class is defined in java.util package and implementation of Dictionary. It maps keys to the values. You can use any non-null object as a key and as a value. When you increase the entries in the Hashtable, the product of the load factor and the present capacity is increased automatically as it calls the rehash method.

Hashtable defines four constructor -

  • Hashtable( )
  • Hashtable(int initialCapacity)
  • Hashtable(int initialCapacity, float loadFactor)
  • Hashtable(Map m)

Hashtable defines many method, Here we are defining some of them -

  • elements() : This method returns an enumeration of the values in your Hashtable.
  • get(Object key) : It returns the value mapped to the given key.
  • keys() : It returns an enumeration of the keys in the Hashtable
  • put(Object key, Object value) : This method maps the given key to the given value in your Hashtable.
  • rehash() : It increases capacity of Hashtable and also reorganizes internally.

Example :

package collection;

import java.util.Enumeration;
import java.util.Hashtable;

public class HashTableExample {
	public static void main(String[] args) {
		Hashtable ht = new Hashtable();
		Enumeration student;
		String name;
		int roll;
		ht.put("Roxi", 1);
		ht.put("Jackson", 2);
		ht.put("Mandy", 4);
		ht.put("Tara", 5);

		student = ht.keys();
		while (student.hasMoreElements()) {
			name = (String) student.nextElement();
			System.out.println(name + ": " + ht.get(name));
		}
		System.out.println();

	}
}

Output :

Jackson: 2
Roxi: 1
Tara: 5
Mandy: 4
Advertisement

Related Tags for Java Collection : Hashtable :


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 9, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com