Hashtable Class

java.util.Hashtable;
public class Hashtable<K,V> extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable
Hashtable extends Dictionary implements Map interface. permits null values and the null key. It is synchronized and null values and the null key can not accepted . This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
A Hashtable can only store objects that override the hashCode( ) and equals( ) methods that are defined by Object.
Hashtable class features:
  • A Hashtable cannot contain duplicate key.
  • It neither cantain null key nor null value .
  • It is synchronized
  • It does not guarantee that the insertion order will remain constant over time.
  • hashCode() and equals() methods is used for its elements .
  • Data structure is based on Hash Table .
Constructor of Hashtable class
  1. public Hashtable( )
  2. public Hashtable(Map m)
  3. public Hashtable(int capacity)
  4. public Hashtable(int capacity, float fillRatio)
Example of Hashtable
					 
import java.util.Hashtable; import java.util.Set; import java.util.Enumeration; class HashTableDemo { public static void main(String args[]) { Hashtable ht = new Hashtable(); Enumeration elements; String str; double bal; ht.put("Sunil",134.34); ht.put("Anand", 1023.22); ht.put("Sanjeev",15678.00); ht.put("Sujeet", 5647.22); // Show all elements in hash table. elements = ht.keys(); // get iterator while(elements.hasMoreElements()) { str = (String) elements.nextElement(); System.out.println(str + ": " + ht.get(str)); } System.out.println(); // Add 1350.67 into Sunil value bal = ((Double)ht.get("Sunil")).doubleValue(); ht.put("Sunil", new Double(bal+1350.67)); System.out.println("Sunil new Value: " + ht.get("Sunil")); } }
Output

Anand: 1023.22
Sujeet: 5647.22
Sanjeev: 15678.0
Sunil: 134.34

Sunil new Value: 1485.01

Download this example
Hashtable Example of set-view
Hashtable does not directly support iterators. So, we can retrieve set-views of the hash table, which permits the use of iterators. To do so, you simply use one of the collection-view methods defined by Map, such as entrySet( ) or keySet( ).
					 
import java.util.Hashtable; import java.util.Set; import java.util.Iterator; class HashTableSetViewDemo { public static void main(String args[]) { Hashtable ht = new Hashtable(); String str; double bal; ht.put("Sunil",134.34); ht.put("Anand", 1023.22); ht.put("Sanjeev",15678.00); ht.put("Sujeet", 5647.22); // show all values in hashtable Set set = ht.keySet(); // get set-view of keys // get iterator Iterator itr = set.iterator(); while(itr.hasNext()) { str = (String) itr.next(); System.out.println(str + ": " + ht.get(str)); } System.out.println(); // Add 1350.67 into Sunil value bal = ((Double)ht.get("Sunil")).doubleValue(); ht.put("Sunil", new Double(bal+1350.67)); System.out.println("Sunil new Value: " + ht.get("Sunil")); } }
Output

Anand: 1023.22
Sujeet: 5647.22
Sanjeev: 15678.0
Sunil: 134.34

Sunil new Value: 1485.01

Download this example
 



 

Latest Tutorial


Most Visited Tutorial