RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.



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