Sunday, 14 June 2015

How to get elements using Enumeration from Hashtable

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

public class HashTableGetEnumeration {

    public static void main(String[] args) {

        Hashtable<Integer, String> ht = new Hashtable<Integer, String>();

        /*
         * Maps the specified key to the specified value in this hashtable
         */
        ht.put(1, "java");
        ht.put(2, "jersey");
        ht.put(3, "spring");
        ht.put(4, "c");
        ht.put(5, "php");

        // Returns an enumeration of the keys in this hashtable

        Enumeration<Integer> keys = ht.keys();

        while (keys.hasMoreElements()) {
            int key = keys.nextElement();

            System.out.println(" key = " + key + " value = " + ht.get(key));

        }

    }

}



Output:

 key = 5 value = php

 key = 4 value = c

 key = 3 value = spring

 key = 2 value = jersey

 key = 1 value = java

No comments:

Post a Comment