Java Collections Part 6: Hashtable

Hashtable:
  • A Hashtable is an array of list. Each list is known as a bucket. 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.
Code:
  1. import java.util.*;  
  2. public class HashTableDemo {  
  3.   
  4.     public static void main(String[] args) {  
  5.         // TODO Auto-generated method stub  
  6.           
  7.         Hashtable<Integer, String>htable=new Hashtable<>();  
  8.         htable.put(100"Sujan");  
  9.         htable.put(103"Shakil");  
  10.         htable.put(102"Shimul");  
  11.         htable.put(104"Badhon");  
  12.         print(htable);  
  13.           
  14.         htable.remove(104);  
  15.         System.out.println("\nAfter removing:");  
  16.         print(htable);  
  17.           
  18.         htable.replace(100"Hasan Sujan");  
  19.         htable.replace(102"Hossain Shimul");  
  20.         System.out.println("\nAfter replacing:");  
  21.         print(htable);  
  22.           
  23.   
  24.     }  
  25.     public static void print(Hashtable<Integer, String>hashtable) {  
  26.         for(Map.Entry m:hashtable.entrySet()){  
  27.             System.out.println(m.getKey()+" "+m.getValue());  
  28.         }  
  29.     }  
  30.   
  31. }  
Share:

0 Comments:

Post a Comment