Java Programing laungage

java.util Projects

java.util Project 1

Hash Table Implementation

In This programming tutorials, we are going to explains the implementation of the hash table. in this page of tutorials we try to know What is the hash table and how to create it.

Previous Home Next

Hash Table holds the records according to the unique key value. It stores the non-contiguous key for several values. Hash Table is created using an algorithm (hashing function) to store the key and value regarding to the key in the hash bucket.

If we want to store value with new key and if that key is already exists in hash bucket then this situation is known as collision occurs which is the problem in the hash table i.e. maintained by the hashing function. The drawback is that hash tables require a little bit more memory, and that you can not use the normal list procedures for working with them.

Code Description

Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>()

This code is used to creates the instance of the Hashtable class. This code is using the type checking of the elements which will be held by the hash table.

hashTable.put(key, in.readLine())

This method is used to put the values in the hash table regarding to the unique key. This method takes two arguments in which, one is the key and another one is the value for the separate key.

Map<Integer, String> map = new TreeMap<Integer, String>(hashTable)

This code is used to creates an instance of the TreeMap for the hash table which name is passed through the constructor of the TreeMap class. This code is used to create the Map with the help of the instance of the TreeMap class in the Map interface. This map has been created in the program for showing several values and it's separate key present in the hash table. This code has used the type checking.


package r4r;
import java.util.*;
public class hashtabletests {
public static void main(String[] args) throws IOException{
 int key;
 BufferedReader br = new BufferedReader(new InputStreamReader(;
System.out.print("How many elements want to enter in the hash table : ");
int n = Integer.parseInt(br.readLine());
Hashtable<Integer, String> hash = new Hashtable<Integer, String>();
for(int i = 0; i < n; i++){
System.out.print("Enter key for hash table : ");
key = Integer.parseInt(br.readLine());
System.out.print("Enter the value for the key : ");
hash.put(key, br.readLine());
Map<Integer, String> map1 = new TreeMap<Integer, String>(hash);
catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a right value.");
System.out.println("Enter a numeric value only.");

Previous Home Next