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[] args) throws IOException{
Hashtable<String,Object> titleBox = new Hashtable<String,Object>
(13, 0.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.