Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Simple Hash Table implementation in Java

This section describes the complete Hash Table implementation from the basic in Java.

Ads

Tutorials   
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
Ads

Simple Hash Table implementation in Java

     

This section describes the complete Hash Table implementation from the basic in Java. In this section, you will see how to store some information and retrieve when it need to search for checking whether the given title exists or not. This section is very helpful for your java application for which the following program supports for working the software in very efficient manner.

Program Description:

Here, an example with complete code is provided for the best illustration of the way of hash table manipulation in Java. How to put some elements with or without any description and retrieve easily when it need. Given program is based on the management of CDs of songs having song's title and singers name of the song. Whenever one needs to search the CD from the title and get whether the given CD's name exists or not in the your stock can be managed by the Hash Table.

Code Description:

HashTable.put(Object key, Object value):
This method of the HashTable class helps to store some elements with appropriate description. This method takes two arguments one is the key of the hash table and another is the value corresponding to the specified key.

HashTable.containsKey(Object key):
This is also the method of the HashTable class which is used for checking whether the specified key (passed as a parameter of the method) exists or not in the hash table. This method returns the boolean value either true or false on the basis of the existence of the key in the hash table.

Here is the code of the program:

import java.util.*;
import java.io.*;

class Disc{
  public String title;
  public String singer;

  Disc(String t, String s) {
  title = t;
  singer = s;
  }
  public String toString(){
  return ("'" + title + "' by " + singer);
  }
}
public class HashTableManipulation{
  public static void print(String msg, Hashtable box, boolean all) {
  if (msg != null)
  System.out.print(msg + ": ");
  if (box.isEmpty())
  System.out.println("The Title Box is empty");
  else {
  System.out.println("There are " + box.size() " discs in the 
Title Box:"
);
  for(Enumeration e = (all ? box.elements() : box.keys());
  e.hasMoreElements()
  System.out.println("\t" + e.nextElement()));
  }
  }
  public static void main (String[] argsthrows IOException{
  Hashtable<String,Object> titleBox = new Hashtable<String,Object>
(
130.5f);
  Disc houndDog;
  titleBox.put("Hamein tumse pyar kitna-(Bollywood)", houndDog 
new Disc("Hamein tumse pyar kitna-(Bollywood)""Kishore Kumar"));
  titleBox.put("Main nikla gaddi le ke-(Bollywood)"new Disc("Main nikla gaddi le ke- (Bollywood)""Udit Narayan"));
titleBox.put("Main hoon na-(Bollywood)"new Disc("Main hoon na-(Bollywood)","Sonu Nigam"));
titleBox.put
("Dard jab had se gujarta hai to ga lete hain-(Bollywood)"
new Disc(
"Dard jab had se gujarta hai to ga lete hain-(Bollywood)"
"Kumar Sanu"));
  print("titleBox after adding 4 titles", titleBox, true);
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter title for search: ");
  String strTitle = in.readLine();
  System.out.println(strTitle + " is " 
(
titleBox.containsKey(strTitle"" "not ")
" in the Title Box");
  titleBox.clear();
  print("Title Box after clearing it", titleBox, true);
  }
}

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Simple Hash Table implementation in Java

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

Advertisements

Ads
 

Discuss: Simple Hash Table implementation in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
chandru
November 12, 2011
Very useful....

This program is very useful for my project Thank you

Ads

 

Ads