Sunday, October 26, 2014

Java Program to demonstrate HashTable


HashTableExample.java:
import java.util.Hashtable;
import java.util.Map;
 
public class HashTableExample {
 
   public static void main(String[] args) {
       Map vehicles = new Hashtable();
        
       // Add some vehicles.
       vehicles.put("BMW", 5);
       vehicles.put("Mercedes", 3);
       vehicles.put("Audi", 4);
       vehicles.put("Ford", 10);
        
       System.out.println("Total vehicles: " + vehicles.size());
        
       // Iterate over all vehicles, using the keySet method.
       for(String key: vehicles.keySet())
           System.out.println(key + " - " + vehicles.get(key));
       System.out.println();
        
       String searchKey = "Audi";
       if(vehicles.containsKey(searchKey))
           System.out.println("Found total " + vehicles.get(searchKey) + " "
                   + searchKey + " cars!\n");
        
       // Clear all values.
       vehicles.clear();
        
       // Equals to zero.
       System.out.println("After clear operation, size: " + vehicles.size());
 
       // The next statements throw a NullPointerException, if uncommented.
       //vehicles.put("Nissan", null);
       //vehicles.put(null, 6);
   }
}
A sample execution is shown below:
Total vehicles: 4
Audi - 4
Ford - 10
BMW - 5
Mercedes - 3
Found total 4 Audi cars!
After clear operation, size: 0

No comments:

Post a Comment