Java Hashtable

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Hashtable class

Java Hashtable class implements a hashtable, which maps keys to values. It inherits Dictionary class and implements the Map interface.

The important points about Java Hashtable class are:

  • A Hashtable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.

  • It contains only unique elements.

  • It may have not have any null key or value.

  • It is synchronized.

Hashtable class declaration

Let's see the declaration for java.util.Hashtable class.

  1. public class Hashtable extends Dictionary implements Map, Cloneable, Serializable  

Hashtable class Parameters

Let's see the Parameters for java.util.Hashtable class.

  • K: It is the type of keys maintained by this map.

  • V: It is the type of mapped values.

Constructors of Java Hashtable class

Constructor

Description

Hashtable()

It is the default constructor of hash table it instantiates the Hashtable class.

Hashtable(int size)

It is used to accept an integer parameter and creates a hash table that has an initial size specified by integer value size.

Hashtable(int size, float fillRatio)

It is used to create a hash table that has an initial size specified by size and a fill ratio specified by fillRatio.

Methods of Java Hashtable class

Method

Description

void clear()

It is used to reset the hash table.

boolean contains(Object value)

This method return true if some value equal to the value exist within the hash table, else return false.

boolean containsValue(Object value)

This method return true if some value equal to the value exists within the hash table, else return false.

boolean containsKey(Object key)

This method return true if some key equal to the key exists within the hash table, else return false.

boolean isEmpty()

This method return true if the hash table is empty; returns false if it contains at least one key.

void rehash()

It is used to increase the size of the hash table and rehashes all of its keys.

Object get(Object key)

This method return the object that contains the value associated with the key.

Object remove(Object key)

It is used to remove the key and its value. This method return the value associated with the key.

int size()

This method return the number of entries in the hash table.

Java Hashtable Example

  1. import java.util.*;  
  2. class TestCollection16{  
  3.  public static void main(String args[]){  
  4.   Hashtable<Integer,String> hm=new Hashtable<Integer,String>();  
  5.   
  6.   hm.put(100,"Amit");  
  7.   hm.put(102,"Ravi");  
  8.   hm.put(101,"Vijay");  
  9.   hm.put(103,"Rahul");  
  10.   
  11.   for(Map.Entry m:hm.entrySet()){  
  12.    System.out.println(m.getKey()+" "+m.getValue());  
  13.   }  
  14.  }  
  15. }  

Test it Now

Output:

103 Rahul
102 Ravi
101 Vijay
100 Amit

Java Hashtable Example: remove()

  1. import java.util.*;  
  2. public class HashtableExample {  
  3.    public static void main(String args[]) {  
  4.    // create and populate hash table  
  5.    Hashtable<Integer, String> map = new Hashtable<Integer, String>();           
  6.    map.put(102,"Let us C");  
  7.    map.put(103"Operating System");  
  8.    map.put(101"Data Communication and Networking");  
  9.    System.out.println("Values before remove: "+ map);    
  10.    // Remove value for key 102  
  11.    map.remove(102);  
  12.    System.out.println("Values after remove: "+ map);  
  13.    }      
  14. }  

Output:

Values before remove: {103=Operating System, 102=Let us C, 101=Data Communication and Networking}
Values after remove: {103=Operating System, 101=Data Communication and Networking}

Java Hashtable Example: Book

  1. import java.util.*;    
  2. class Book {    
  3. int id;    
  4. String name,author,publisher;    
  5. int quantity;    
  6. public Book(int id, String name, String author, String publisher, int quantity) {    
  7.     this.id = id;    
  8.     this.name = name;    
  9.     this.author = author;    
  10.     this.publisher = publisher;    
  11.     this.quantity = quantity;    
  12. }    
  13. }    
  14. public class HashtableExample {    
  15. public static void main(String[] args) {    
  16.     //Creating map of Books    
  17.     Map<Integer,Book> map=new Hashtable<Integer,Book>();    
  18.     //Creating Books    
  19.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);    
  20.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);    
  21.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);    
  22.     //Adding Books to map   
  23.     map.put(1,b1);  
  24.     map.put(2,b2);  
  25.     map.put(3,b3);      
  26.     //Traversing map  
  27.     for(Map.Entry<Integer, Book> entry:map.entrySet()){    
  28.         int key=entry.getKey();  
  29.         Book b=entry.getValue();  
  30.         System.out.println(key+" Details:");  
  31.         System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);   
  32.     }    
  33. }    
  34. }    

Output:

3 Details:
103 Operating System Galvin Wiley 6
2 Details:
102 Data Communications & Networking Forouzan Mc Graw Hill 4
1 Details:
101 Let us C Yashwant Kanetkar BPB 8