Problem : Create a dynamic HashTable that can add any number of elements of any object.
Program :Step 1 - CodingCreate 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
Step 3 - Testing
OutputSize of HashTable is : 4Value 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 FAQWhat is HashTable?
Is HashTable thread-safe?Yes, it is synchronized.HashMap vs Hashtable?
Most interesting methods?
<<Previous | Next>> |