How to iterate the HashTable?

This example shows how to read elements from Hashtable. You can iterate each and every element by getting all keys as set object. Using each element as a key from set, you can values from Hashtable.


								                        
package com.mindclues;
 
import java.util.Hashtable;
import java.util.Set;
 
public class HashtableReading {
 
    public static void main(String a[]){
         
        Hashtable htm = new Hashtable();
        //add key-value pair to Hashtable
        htm .put("first", "apple");
        htm .put("second", "mango");
        htm .put("third","banana");
        System.out.println(htm );
        Set keys = htm .keySet();
        for(String key: keys){
            System.out.println("Value of "+key+" is: "+htm.get(key));
        }
    }
}
							

								                        
second=mango
first=apple
third=banana

Value of first is: apple
Value of second is: mango
Value of third is: banana

							

Related Articles

post a comment