Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Hashtable Example

  1. /*
  2. Java Hashtable example.
  3. This Java Hashtable example describes the basic operations performed on the Hashtable.
  4. */
  5.  
  6. import java.util.Hashtable;
  7. import java.util.Enumeration;
  8.  
  9. public class HashtableExample{
  10.    
  11.     public static void main(String args[]){
  12.        
  13.         // constructs a new empty hashtable with default initial capacity        
  14.         Hashtable hashtable = new Hashtable();
  15.        
  16.         /*
  17.        
  18.         To specify initial capacity, use following constructor        
  19.         Hashtable hashtable = new Hashtable(100);
  20.        
  21.         To create hashtable from map use following constructor        
  22.         Hashtable hashtable = new Hashtable(Map myMap);
  23.        
  24.          */
  25.        
  26.         hashtable.put("One", new Integer(1)); //adding value to Hashtable
  27.        
  28.         hashtable.put("Two", new Integer(2));
  29.        
  30.         hashtable.put("Three", new Integer(3));
  31.        
  32.         /*
  33.         IMPORTANT : We CAN NOT add primitives to the Hashtable. We have to wrap it into one
  34.         of the wrapper classes before adding.
  35.         */
  36.        
  37.         /*
  38.        
  39.         To copy all key - value pairs from Map to Hashtable use putAll method.
  40.        
  41.         Signature of putAll method is,        
  42.         void putAll(Map m)
  43.        
  44.         */
  45.        
  46.         //get number of keys present in the hashtable
  47.         System.out.println("Hashtable contains " + hashtable.size() + " key value pairs.");
  48.        
  49.         /*
  50.        
  51.         To check whether Hashtable is empty or not, use isEmpty() method.        
  52.         isEmpty() returns true is Hashtable is empty, otherwise false.
  53.        
  54.         */
  55.        
  56.         /*
  57.        
  58.         Finding particular value from the Hashtable :
  59.        
  60.         Hashtable's contains method returns boolean depending upon the presence of the value in given hashtable
  61.        
  62.         Signature of the contains method is,        
  63.         boolean contains(Object value)
  64.        
  65.         */
  66.        
  67.         if( hashtable.contains(new Integer(1))){
  68.             System.out.println("Hashtable contains 1 as value");
  69.         }else{
  70.             System.out.println("Hashtable does not contain 1 as value");
  71.         }
  72.        
  73.         /*
  74.        
  75.         Finding particular Key from the Hashtable :
  76.        
  77.         Hashtable's containsKey method returns boolean depending upon the presence of the
  78.         key in given hashtable
  79.        
  80.         Signature of the method is,        
  81.         boolean containsKey(Object key)
  82.        
  83.         */
  84.        
  85.         if(hashtable.containsKey("One")){
  86.             System.out.println("Hashtable contains One as key");
  87.         }else{
  88.             System.out.println("Hashtable does not contain One as value");
  89.         }
  90.        
  91.         /*
  92.        
  93.         Use get method of Hashtable to get value mapped to particular key.
  94.        
  95.         Signature of the get method is,        
  96.         Object get(Object key)
  97.        
  98.         */
  99.        
  100.         Integer one = (Integer) hashtable.get("One");
  101.        
  102.         System.out.println("Value mapped with key \"One\" is " + one);
  103.        
  104.         /*
  105.         IMPORTANT:  get method returns an Object, so we need to downcast it.
  106.         */
  107.        
  108.         /*
  109.        
  110.         To get all keys stored in Hashtable use keys method.
  111.        
  112.         Signature of the keys method is,        
  113.         Enumeration keys()
  114.        
  115.         To get Set of the keys use keySet() method instead.        
  116.         Set keySet()
  117.        
  118.         */
  119.        
  120.         System.out.println("Retrieving all keys from the Hashtable");
  121.        
  122.         Enumeration e = hashtable.keys();
  123.        
  124.         while( e. hasMoreElements() ){        
  125.             System.out.println(e.nextElement());
  126.         }
  127.        
  128.         /*
  129.        
  130.         To get all values stored in Hashtable use elements method.
  131.        
  132.         Signature of the elements method is,        
  133.         Enumeration elements()
  134.        
  135.         To get Set of the values use entrySet() method instead.        
  136.         Set entrySet()
  137.        
  138.         */
  139.        
  140.         System.out.println("Retrieving all values from the Hashtable");
  141.        
  142.         e = hashtable.elements();
  143.        
  144.         while(e. hasMoreElements()){        
  145.             System.out.println(e.nextElement());    
  146.         }
  147.        
  148.         /*
  149.        
  150.         To remove particular key - value pair from the Hashtable use remove method.
  151.        
  152.         Signature of remove method is,        
  153.         Object remove(Object key)
  154.        
  155.         This method returns value that was mapped to the given key, otherwise null if mapping not found.
  156.        
  157.         */
  158.        
  159.         System.out.println( hashtable.remove("One") + " is removed from the Hashtable.");
  160.        
  161.     }
  162.  
  163. }
  164.  
  165. /*
  166. OUTPUT of the above given Java Hashtable Example would be :
  167.  
  168. Hashtable contains 3 key value pair.
  169. Hashtable contains 1 as value
  170. Hashtable contains One as key
  171. Value mapped with key "One" is 1
  172. Retrieving all keys from the Hashtable
  173. One
  174. Three
  175. Two
  176. Retrieving all values from the Hashtable
  177. 1
  178. 3
  179. 2
  180. 1 is removed from the Hashtable.
  181.  
  182. */
Your rating: None Average: 4.3 (51 votes)
Share this