Easy learning with example program codes

Hashtable in java


Hashtable extends Dictionary class and implements Map interface. It contains elements in key-value pair. It not allowed duplicate key. It is synchronized. It can’t contain null key or value. It uses hashcode() method for finding the position of the elements.

Hashtable example:

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 * This class is used to show the Hashtable functionality.
 * @author codesjava
public class HashtableTest {
	public static void main(String args[]){
		//Create Hashtable object.
		Hashtable hashtable = new Hashtable();
		//Add objects to the Hashtable.
		//Print the Hashtable object.
		System.out.println("Hashtable elements:");
		//Get iterator
		Set set=hashtable.entrySet();  
		Iterator iterator=set.iterator();  
		//Print the Hashtable elements using iterator.
		System.out.println("Hashtable elements using iterator:");
		   Map.Entry mapEntry=(Map.Entry);	
		   System.out.println("Key: " + mapEntry.getKey() + 
		       ", " + "Value: " + mapEntry.getValue());   


Hashtable elements:
{5=Sahdev, 4=Himanshu, 3=Rajesh, 2=Bharat, 1=Richi}
Hashtable elements using iterator:
Key: 5, Value: Sahdev
Key: 4, Value: Himanshu
Key: 3, Value: Rajesh
Key: 2, Value: Bharat
Key: 1, Value: Richi

Download this example.
Next Topic: ListIterator interface in java with example.
Previous Topic: Properties class in java with example.

Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki