share

Thursday, 11 June 2015

How to get all keys of HashTable

import java.util.Hashtable;
import java.util.Set;

public class HashTableGetAllKeys {

    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 a Set view of the keys contained in this map.
         */
        Set<Integer> keys =ht.keySet();
       
        for (Integer key : keys) {
            System.out.println("key ="+key);
        }
   
    }
   
   
}


Output:

key =5

key =4

key =3

key =2

key =1



No comments:

Post a Comment