Hashtable
Hashtable is a data structure used to implement an associative array, a structure that can map keys to values. It can provide a very efficient way to search for items in large amounts of data, particularly data that is not otherwise easily searchable. Hashtable can contains only unique elements and may not have any null key or value. It is almost similar to HashMap, but is synchronized. Dictionary is an abstract base class of Hashtable. However, Java 2 re-engineered Hashtable so that it also implements the Map interface and making it a member of the Java Collections Framework.
How Hashtable works?
The hash algorithm is used to generate an index into that array based on the values of the item that will be stored in the array. The maximum size of this array is typically smaller than the number of items in the set of possible values for the type of data being stored in the hashtable. When an element is added to the Hashtable , the element is placed into a bucket based on the hash code of the key. Each bucket can have multiple records which are organized in a particular order. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element. The load factor of a Hashtable determines the maximum ratio of elements to buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. An instance of Hashtable has two parameters that affect its performance: initial capacity and load factor . The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.The following Java program illustrates several of the methods supported by this Hashtable collection Framework
import java.util.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
//How to Create Hashtable?
Hashtable <Integer,String> days = new Hashtable <Integer,String>();
//How to Add Key/Value pairs in Hashtable?
days.put(1,"Sunday");
days.put(2,"Monday");
days.put(3,"Tuesday");
days.put(4,"Wednesday");
days.put(5,"Thursday");
days.put(6,"Friday");
days.put(7,"Saturday");
//How to iterate through Hashtable?
for(Map.Entry m:days.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
//How to remove specific item from Hashtable?
days.remove(3);
Set<Map.Entry<Integer,String>> set = days.entrySet();
for (Map.Entry<Integer,String> sg : set) {
System.out.println("Key :"+sg.getKey() + " Value :"+days.get(sg.getKey()));
}
//How to search a key in Hashtable?
Integer key=4;
if(days.containsKey(key)){
System.out.println("Key " + key + " found");
}else{
System.out.println("Key " + key+ " does not exist");
}
//How to get Key from its Value in Hashtable?
Integer iKey= null;
String value="Monday";
for(Map.Entry entry: days.entrySet()){
if(value.equals(entry.getValue())){
iKey = (Integer)entry.getKey();
break; //breaking because its one to one map
}
}
System.out.println("Found Key : "+ iKey +" value: " + value);
//How remove all item from hashtable?
days.clear();
//How to find the size of Hashtable?
System.out.println("After remove: "+ days.size());
}
}
Related Topics