Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎

HashTable

Problem : Create a dynamic HashTable that can add any number of elements of any object.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestHashTable.java  and copy below contents.

import java.util.*;


/*
 * A program of HashTable.
 */

public class TestHashTable {

    public static void main(String[] args) {

        Hashtable hTable = new Hashtable();

        /*
         * You can insert any object in the HasTable with key and value pair.
         * Here it is string
         */
        hTable.put("RN1001", new Integer(890));
        hTable.put("RN1002", new Integer(900));
        hTable.put("RN1003", new Integer(780));
        hTable.put("RN1004", new Integer(950));

        // Display the size of HashTable
        System.out.println("Size of HashTable is : " + hTable.size());

        // Show the value at given key
        System.out.println("Value at given key : " + hTable.get("RN1003"));

        Enumeration en = hTable.elements();

        System.out.println("Print All Elements with help of Enumeration ");

        while (en.hasMoreElements()) {
            Object oo = en.nextElement();
            System.out.println(" From Enumeration -- " + oo);
        }

        // Remove value at given key
        System.out.println("Remove value at given key : "
                + hTable.remove("RN1003"));

        // Will remove all elements
        hTable.clear();

        // Show that HashTable is empty
        System.out.println("Is Empty : " + hTable.isEmpty());

    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestHashTable.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestHashTable.java with help of 
    • javac TestHashTable.java command. Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestHashTable command.

Output

Size of HashTable is : 4
Value at given key : 780
Print All Elements with help of Enumeration
 From Enumeration -- 950
 From Enumeration -- 780
 From Enumeration -- 900
 From Enumeration -- 890
Remove value at given key : 780
Is Empty : true

FAQ

What is HashTable?

  1. This class maps keys to values. Any non-null object can be used as a key or as a value.
  2. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode() method and the equals()method.

Is HashTable thread-safe?

Yes, it is synchronized.

HashMap vs Hashtable?

 # HashMap Hashtable
 1 Not Thread Safe
 Thread Safe
 2 Asynchronous
 Synchronous
 3 Hashtable is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException.

Most interesting methods?

  • put(key,value) - Maps the specified key to the specified value in this hashtable.
  • get(key) - Returns the value to which the specified key is mapped in this hashtable.
  • remove(key) -  Removes the key (and its corresponding value) from this hashtable.
  • size() - Returns the number of keys in this hashtable.
  • keySet() - Returns a Set view of the keys contained in this Hashtable.
  • isEmpty() -  Tests if this hashtable maps no keys to values.
  • clear() - Clears this hashtable so that it contains no keys.


<<Previous | Next>>
ċ
TestHashTable.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:28 PM
Comments