Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Java Hashtable example.
- This Java Hashtable example describes the basic operations performed on the Hashtable.
- */
- import java.util.Hashtable;
- import java.util.Enumeration;
- public class HashtableExample{
- public static void main(String args[]){
- // constructs a new empty hashtable with default initial capacity
- Hashtable hashtable = new Hashtable();
- /*
- To specify initial capacity, use following constructor
- Hashtable hashtable = new Hashtable(100);
- To create hashtable from map use following constructor
- Hashtable hashtable = new Hashtable(Map myMap);
- */
- hashtable.put("One", new Integer(1)); //adding value to Hashtable
- hashtable.put("Two", new Integer(2));
- hashtable.put("Three", new Integer(3));
- /*
- IMPORTANT : We CAN NOT add primitives to the Hashtable. We have to wrap it into one
- of the wrapper classes before adding.
- */
- /*
- To copy all key - value pairs from Map to Hashtable use putAll method.
- Signature of putAll method is,
- void putAll(Map m)
- */
- //get number of keys present in the hashtable
- System.out.println("Hashtable contains " + hashtable.size() + " key value pairs.");
- /*
- To check whether Hashtable is empty or not, use isEmpty() method.
- isEmpty() returns true is Hashtable is empty, otherwise false.
- */
- /*
- Finding particular value from the Hashtable :
- Hashtable's contains method returns boolean depending upon the presence of the value in given hashtable
- Signature of the contains method is,
- boolean contains(Object value)
- */
- if( hashtable.contains(new Integer(1))){
- System.out.println("Hashtable contains 1 as value");
- }else{
- System.out.println("Hashtable does not contain 1 as value");
- }
- /*
- Finding particular Key from the Hashtable :
- Hashtable's containsKey method returns boolean depending upon the presence of the
- key in given hashtable
- Signature of the method is,
- boolean containsKey(Object key)
- */
- if(hashtable.containsKey("One")){
- System.out.println("Hashtable contains One as key");
- }else{
- System.out.println("Hashtable does not contain One as value");
- }
- /*
- Use get method of Hashtable to get value mapped to particular key.
- Signature of the get method is,
- Object get(Object key)
- */
- Integer one = (Integer) hashtable.get("One");
- System.out.println("Value mapped with key \"One\" is " + one);
- /*
- IMPORTANT: get method returns an Object, so we need to downcast it.
- */
- /*
- To get all keys stored in Hashtable use keys method.
- Signature of the keys method is,
- Enumeration keys()
- To get Set of the keys use keySet() method instead.
- Set keySet()
- */
- System.out.println("Retrieving all keys from the Hashtable");
- Enumeration e = hashtable.keys();
- while( e. hasMoreElements() ){
- System.out.println(e.nextElement());
- }
- /*
- To get all values stored in Hashtable use elements method.
- Signature of the elements method is,
- Enumeration elements()
- To get Set of the values use entrySet() method instead.
- Set entrySet()
- */
- System.out.println("Retrieving all values from the Hashtable");
- e = hashtable.elements();
- while(e. hasMoreElements()){
- System.out.println(e.nextElement());
- }
- /*
- To remove particular key - value pair from the Hashtable use remove method.
- Signature of remove method is,
- Object remove(Object key)
- This method returns value that was mapped to the given key, otherwise null if mapping not found.
- */
- System.out.println( hashtable.remove("One") + " is removed from the Hashtable.");
- }
- }
- /*
- OUTPUT of the above given Java Hashtable Example would be :
- Hashtable contains 3 key value pair.
- Hashtable contains 1 as value
- Hashtable contains One as key
- Value mapped with key "One" is 1
- Retrieving all keys from the Hashtable
- One
- Three
- Two
- Retrieving all values from the Hashtable
- 1
- 3
- 2
- 1 is removed from the Hashtable.
- */

Great explanation
thanks
Thanks . Great Explanation
Thanks. quite strictly way to
The explanation is too good
An example that I was looking
interesting
thank you
thanks dear
Thanks dude
thank you for the nice
anyone can understand in one
great........................
Great explanation thanks
its useful for the beginners
A wounderful example for this
Best Exampale
Best Exampale
nice one
A holistic understanding into
System.out.println("Hashtable
good way to explain things
This tutorial is outdated now
Keep it up man good work.
thx, it is useful
thanxs good way to explain
Agree with Anonymous here,
thanks your explanation is so
It's good. Thanks
great thanks for the
thanks a lot
good job.....
thanx
great job of knowledge
Thanx a lot.... Very
Thanks realy helps
Thanks Awesome :-)
Straight and clear. thank you
Best help which I've ever
very nicely explained but do
this is best to my level
this is best to my level
very straight forward! Nicely
Great tutorial...Good job
very very bad ? I feel for
Good job! Thanks!
I helped me so much, thanks!
thanks
good explanation and easy to
Nice Explanation Very
Post new comment