Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Sep 16, 1999

Traverse a Hashtable

Java hashtables are widely used to maintain a mapping relationship between objects. It is easy to store, and retrieve an object from a hashtable using an arbitrary key. Sometimes, it is useful to get an enumeration of all the keys used on a hashtable. This can be done using the keys() method of java.util.Hashtable, as in:
 
    java.util.Enumeration keys = aHashTable.keys();
    //aHashTable is an instance of java.util.Hashtable
Having all the keys used to map objects to a hashtable, we can traverse the entire hashtable and print out key-value pairs, as in:
 
    while( keys.hasMoreElements() ) 
    {
        Object aKey = keys.nextElement();
        Object aValue = aHashTable.get(aKey);
        System.out.println("Key: \""+aKey.toString()+"\" 
has value of: \"" +aValue.toString()+"\"");
    }
Behrouz Fallahi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date