Java Hashtable

Hashtables are used to store key-value pairs. Java Hashtable is now integrated into the collection framework in Oracle Java. It is a member of java.util package.  Hash table uses hashing technique to store these key-value pairs. It creates indexes wherever an element is inserted, displayed or deleted.

It uses an array data structure to store the data retrieved from the user. Also, each list in a hash table is called as a ‘bucket’. The position of any bucket can be retrieved by using the hashcode() method. The hashtable class in Java contains all unique elements. 

Using Java Hashtable

The following command can be used to import Hashtable in a program. 

import java.util.HashTable; 
import java, util.* 

can also be used to import all the libraries of the java.util class. This is the general declaration for a Hashtable.

To store and retrieve objects successfully from a Hashtable, the objects used as keys must fall in line with the hashCode method. The Hashtable stores data in just like an associative array.

The following  code snippet shows the ways to declare a new HashTable. 

public class Hashtable<K,V>
extends Dictionary<K,V> 
implements Map<K,V>, CloneableSerializable 

Here, in the above format, K stands for Key of the Hashtable and V is the value associated with that key that will be mapped. Here is an example to show the basic use of Hashtable  in java  to input and get the data  :

Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();

Iterators from the collections class can be used to retrieve data. 

import java.util.*; 
 
class HashExample{  
 public static void main(String args[]){  
    Hashtable<String,Integer> data=new Hashtable<String,Integer>();            
	data.put("CAT",101);
	data.put("DOG",102) ;
	data.put("MAT",103) ;
    data.put("FAT",104) ;

	//we can use the get() method to retrieve the data from the Hashtable passing key to the get method. 

    Integer n = data.get("FAT");
	System.out.println(n);  
 }  
}  
Java Hashtable Example

The data added in a Hashtable can be removed by calling the remove method with Hashtable object and passing the key of the element.

In the below example to remove a record, we can use the htable.remove(20) command as shown in the following example:

import java.util.*; 
 
class HashExample{  
 public static void main(String args[]){  
    Hashtable<Integer, String> htable =  new Hashtable<Integer, String>(); 
 
        htable.put(10, "Samtam"); 
        htable.put(20, "KimKoo"); 
        htable.put(30, "IroDov"); 
        htable.put(40, "AlePlex"); 
        htable.put(50, "OmiVat"); 
		
		System.out.println("Before Deletion");
		for(Map.Entry h:htable.entrySet()){  
				System.out.println(h.getKey()+" "+h.getValue());  
			}  

		htable.remove(20); 
		System.out.println("\nAfter Deletion");
		for(Map.Entry h:htable.entrySet()){  
				System.out.println(h.getKey()+" "+h.getValue());  
			}  
 }  
}  
Remove Element Example

Java Hashtable  and HashMap both are used to store data in key and value pair format but are different on some structural parameters. Documentations of various methods are available in the Java Hashtable class on this link.

Be First to Comment

Leave a Reply

Your email address will not be published.