Showing posts with label enumeration. Show all posts
Showing posts with label enumeration. Show all posts

Friday, January 4, 2008

How to iterate hash table in java

PRINTING HASH TABLE
To print all the elements of a hash table is not simple task, here is a sample code to help you in this regard


import java.util.Hashtable;
import java.util.Enumeration;

// create a new Hashtable
Hashtable h = new Hashtable( );


// add some key/value pairs to the Hashtable
h.put( "LA" , "Lahore" );
h.put( "KR" , "Karachi" );
h.put( "MN" , "Multan" );
h.put( "SDK" , "SADIQABAD" );


// enumerate all the contents of the hashtable
Enumeration keys = h.keys();
while ( keys.hasMoreElements() )
{
key = (String)keys.nextElement();
stateName = (String)h.get( key );
System.out.println( key + " " + stateName );
// prints lines of the form LA Lahore
// in effectively random order.
} // end while