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.5 (42 votes)
Share this
Anonymous's picture

Great explanation

Great explanation
Anonymous's picture

thanks

thanks
Anonymous's picture

Thanks . Great Explanation

Thanks . Great Explanation
Anonymous's picture

Thanks. quite strictly way to

Thanks. quite strictly way to explain the idea
Anonymous's picture

The explanation is too good

The explanation is too good
Anonymous's picture

An example that I was looking

An example that I was looking for. Thank you.
Anonymous's picture

interesting

interesting
Anonymous's picture

thank you

thank you
Anonymous's picture

thanks dear

thanks dear
Anonymous's picture

Thanks dude

Thanks dude
Anonymous's picture

thank you for the nice

thank you for the nice explanation
Anonymous's picture

anyone can understand in one

anyone can understand in one view. thanks . great work
Anonymous's picture

great........................

great..................................
Anonymous's picture

Great explanation thanks

Great explanation thanks
Anonymous's picture

its useful for the beginners

its useful for the beginners .....
Anonymous's picture

A wounderful example for this

A wounderful example for this newbie. Thanks!
Anonymous's picture

Best Exampale

Best Exampale
Anonymous's picture

Best Exampale

Best Exampale
Anonymous's picture

nice one

nice one
Anonymous's picture

A holistic understanding into

A holistic understanding into the Hashtable concept.Thank you.
Anonymous's picture

System.out.println("Hashtable

System.out.println("Hashtable does not contain One as value"); in line 88, that should say "does not contain One as a key" instead, correct?
Anonymous's picture

good way to explain things

good way to explain things precisely
Anonymous's picture

This tutorial is outdated now

This tutorial is outdated now you should add genericity to all the declarations that will allow you not to cast types everytime you want to use a value of the hashtable
Anonymous's picture

Keep it up man good work.

Keep it up man good work.
Anonymous's picture

thx, it is useful

thx, it is useful
Anonymous's picture

thanxs good way to explain

thanxs good way to explain things Regards Ashok
Anonymous's picture

Agree with Anonymous here,

Agree with Anonymous here, Java is now moved from java 4 to java5 and generic is a must known feature. you should update this post to allow generics here , any way good information for anyone new to hashmap. Javin How HashMap works in Java
Anonymous's picture

thanks your explanation is so

thanks your explanation is so perfect!! :D :D
Anonymous's picture

It's good. Thanks

It's good. Thanks
Anonymous's picture

great thanks for the

great thanks for the explanation
Anonymous's picture

thanks a lot

thanks a lot
Anonymous's picture

good job.....

good job.....
Anonymous's picture

thanx

thanx
Anonymous's picture

great job of knowledge

great job of knowledge sharing
Anonymous's picture

Thanx a lot.... Very

Thanx a lot.... Very helpfull.....
Anonymous's picture

Thanks realy helps

Thanks realy helps
Anonymous's picture

Thanks Awesome :-)

Thanks Awesome :-)
Anonymous's picture

Straight and clear. thank you

Straight and clear. thank you !!
Anonymous's picture

Best help which I've ever

Best help which I've ever gotten. Thumbs up for it :D
Anonymous's picture

very nicely explained but do

very nicely explained but do we need type casting even after the concept of autoboxing in following line hashtable.put("Three", new Integer(3));
Anonymous's picture

this is best to my level

this is best to my level
Anonymous's picture

this is best to my level

this is best to my level
Anonymous's picture

very straight forward! Nicely

very straight forward! Nicely done.
Anonymous's picture

Great tutorial...Good job

Great tutorial...Good job Thanks
Anonymous's picture

very very bad ? I feel for

very very bad ? I feel for all of you
Anonymous's picture

Good job! Thanks!

Good job! Thanks!
Anonymous's picture

I helped me so much, thanks!

I helped me so much, thanks!
Anonymous's picture

thanks

thanks
Anonymous's picture

good explanation and easy to

good explanation and easy to understand.
Anonymous's picture

Nice Explanation Very

Nice Explanation Very Useful By Sandesh

Post new comment