Google News
logo
Java - Hash Table
HashTable
1. It is a legacy class introduced in the 1.0 version.
2. Every method is synchronized hence only one thread is allow to access.
3. The performance of the application is low.
4. Null insertion is not possible if we are trying to insert null values we are getting NullPointerException.
Creating Hashtable object:
a). Hashtable ht = new Hashtable();
Here, to store String objects as keys and integer objects as its values, we create the Hashtable object as shown above. We did not mention any capacity for the Hashtable. Hence, the default capacity of this HashMap will be taken as 11 and the load factor as 0.75. Load factor represents at what level this HashMap capacity should be doubled.
For example: capacity * load factor = 11 * 0.75 = 8.25.
This represents that after storing the 8th key-value pair into the HashMap, its capacity will become 22. 

b). Hashtable ht = new Hashtable(60);
The preceding statement creates a Hashtable object (ht) which can be used to store String type keys and Integer type values. The initial capacity of Hashtable object (ht) is declared as 60. 

c). Hashtable ht = new Hashtable(80, 0.5);
The preceding statement creates a Hashtable object (ht) which can be used to store String type keys and Integer type values. The initial capacity of Hashtable object (ht) is declared as 60 and the load factor as 0.5.
import java.util.*; 
class Test 
{ 
public static void main(String[] args) 
{ 
Hashtable h=new Hashtable(); 
h.put("hyd",100); 
h.put("bang",200); 
h.put("pune",300); 
System.out.println(h); 
System.out.println(h.contains(300));//true 
System.out.println(h.containsValue(500));//false 
Collection c=h.values();
System.out.println(c); 
Set c1=h.keySet(); 
System.out.println(c1); 
} 
}
Output :
{hyd=100, bang=200, pune=300}
true
false
[100, 200, 300]
[hyd, bang, pune]