Creating a Hash Table

This section explains the implementation of the hash table. What is the hash table and how to create that?



How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value

Creating a Hash Table : Java Util


This section explains the implementation of the hash table. What is the hash table and how to create that? 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 you want to store the value for the new key and if that key is already exists in the hash bucket then the situation 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.

This program simply asks you for the number of entries which have to entered into the hash table and takes one-by-one key and it's value as input. It shows all the elements with the separate key.

Code Description:

Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>():
Above code 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()):
Above method puts 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):
Above code creates an instance of the TreeMap for the hash table which name is passed through the constructor of the TreeMap class. This code creates the Map with the help of the instance of the TreeMap class. 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.

Here is the code of the program:

import java.util.*;

public class HashTable{
  public static void main(String[] argsthrows IOException{
  int key;
  BufferedReader in = new BufferedReader(new InputStreamReader(;
  System.out.print("How many elements you want to enter to
 the hash table : "
  int n = Integer.parseInt(in.readLine());
  Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
  for(int i = 0; i < n; i++){
  System.out.print("Enter key for the hash table : ");
  key = Integer.parseInt(in.readLine());
  System.out.print("Enter value for the key : ");
  hashTable.put(key, in.readLine());
  Map<Integer, String> map = new TreeMap<Integer, String>(hashTable);
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a legal value.");
  System.out.println("Please enter a numeric value.");

Download this example.


Share on Google+Share on Google+

Creating a Hash Table

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Creating a Hash Table   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 23, 2011

how can i construct a cluster tree in document clustering in java using hash table
June 25, 2011

How to use enumeration in hash table
June 25, 2011

How to use enumeration in hash table give some example
November 30, 2011
struts and hibernate integration code

plz send if ur have.....
January 13, 2012

May 29, 2012
Hash table

I have problems with implementing own hash table without take from standard libery. My program need to read from a file and choose the most 100 frequent words. Thank you!
August 11, 2013
Example with collision resolution

Hello Can you please show a simple implementation just like one given above with collision resolution. Specifically with separate chaining. I need this program please help. Thanks
deepak kumar
September 27, 2013

give the brief introduction about applet i know we can used four parameter to draw the rectangle but how can we put in a well manner i don't know so you give the detail description about these topic.