Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.*;
import java.io.*;

public class HashTable{
  public static void main(String[] argsthrows IOException{
    int key;
    try{
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      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);
      System.out.println(map);
    }
    catch(NumberFormatException ne){
      System.out.println(ne.getMessage() " is not a legal value.");
      System.out.println("Please enter a numeric value.");
      System.exit(1);
    }
  }
}

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (post your own) View All Comments Latest 10 Comments:

Your web Site is very Nice.

Posted by N.Boopalan on Tuesday, 07.3.07 @ 17:32pm | #20661

Hashtable is the older collection class... I suggest to use HashMap<K,V> which is the newer equivalent.

HashMap is not synchronized, but Hashtable is, so if you you have thread safe code, it is better to use the newer HashMap<K,V>

You didn't mention it, but TreeMap<K,V> is a specialized type of hash in which the keys are sorted (via an internal binary tree which stays balanced). The K has to implement the natural comparison (compareTo()).

Finally, when you define your own class for the K (key), its ALWAYS good advice to implement 3 methods: equals(), hashCode(), and compareTo(). Why? By having all 3 methods, you can use your K (key) either in a HashMap or in a TreeMap. And make sure that your compareTo() and equals() are consistent with each other. An easy way to accomplish this, is to have your equals() method call the compareTo() method as a helper method.

Posted by dennis.bednar on Tuesday, 12.5.06 @ 20:39pm | #436

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.