Showing posts with label how to convert map to set. Show all posts
Showing posts with label how to convert map to set. Show all posts

Sunday, 14 June 2015

How to get entry set from Hashtable

import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Set;

public class HashtableEntrySet {
   
    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");
       
        //print hashtable
        System.out.println("Hash Table is ="+ht);
       
        //Returns a Set view of the mappings contained in this
        // map
        Set<Entry<Integer, String>> entry = ht.entrySet();
       
        for (Entry<Integer, String> ent : entry) {
            System.out.println(" key = "+ent.getKey()+" value = "+ent.getValue());
        }
    }   
   
}

Output:

Hash Table is ={5=php, 4=c, 3=spring, 2=jersey, 1=java}

key = 5 value = php

key = 4 value = c

key = 3 value = spring

key = 2 value = jersey

key = 1 value = java