HashTable in Java


Prev Tutorial Next Tutorial

HashTable in Java

HashTable is Implementer class of Map interface and extends Dictionary class. HashTable does not allows null key and null values, these elements will be stored in a random order.

Points to Remember

  • HashTable is a legacy class, which will uses hashing technique (the elements will be stored in unsorted or un-order format).
  • HashTable stored the elements in key values formate.
  • HashTable does not allows null keys and null values.
  • HashTable is Synchronized.
  • HashTable allows heterogeneous elements.
  • For retrieve elements from HashTable you can use foreach loop, Iterator Interface and Enumeration.

Note: HashTable class also contains same methods like HashMap.

Difference between HashMap and HashTable

HashMapHashTable
1HashMap is a new class in java API.HashTable is a legacy class in java API.
2HashMap is not Synchronized.HashTable is Synchronized.
3HashMap allows maximum one null key and multiple null value.HashTable does not allows any null key and null values.

Limitations of HashTable

  • Hashtable class object is enable to read the data from property file / resource bundle file.
  • Hashtable class object is unable to develop flexible application.

Example of HashTable

import java.util.*;
class HashTableDemo
{
public static void main(String args[])
{  
   
  HashTable<Integer,String> ht=new HashTable<Integer,String>();  
  ht.put(1,"Deo");  
  ht.put(2,"zen");  
  ht.put(3,"porter");  
  ht.put(4,"piter");  
  
   System.out.println(ht);  
 }  
}  

Output

[1=Deo, 2=zen, 3=porter ,4=piter]

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements