Hash Table Implementation

Objective : To implement a Hash Table

Input:  A set of pairs of keys and values


    • Create a Hash Table
      • Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
    • Insert values in hash table using put(key,value)
      • ht.put(key, value);

  • Get values from hash table using get(key)
    • ht.get(key);
hash table
hash table

Advantage : The search time for any element is O(1) since it uses key to find an element so it takes constant time. But drawback is that it takes extra space.

Complete Code:

import java.util.Hashtable;
public class SimpleHashTable {
int [] a = new int[5];
String [] arrNames = new String[]{"Sumit","Jain","Raghav","Garg","Gaurav","Rishi"};
Hashtable<Integer, String> ht = new Hashtable<Integer, String>();
public void insertValues(){
for(int i=0;i<arrNames.length;i++ ){
public String getValue(int key){
return ht.get(key);
public static void main (String [] args){
SimpleHashTable sht = new SimpleHashTable();
System.out.println("All values inserted");
System.out.println("Employee with ID 1 is "+ sht.getValue(1));
System.out.println("Employee with ID 3 is "+ sht.getValue(6));


All values inserted
Employee with ID 1 is Sumit
Employee with ID 3 is Rishi

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.