Tuesday 3 March 2015

Java : Collection Framework : Hashtable (keySet)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=nytZrO6wug0&list=UUhwKlOVR041tngjerWxVccw

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

/*
 * Example of keySet() method.
 */
public class HashtableExample
{
    public static void main( String[] args )
    {

        Hashtable<String, String> hashtable = new Hashtable<String, String>();

        /*
         * Key = CountryCode,Value = CountryName.
         */
        hashtable.put("AF", "AFGHANISTAN");
        hashtable.put("BE", "BELGIUM");
        hashtable.put("US", "UNITED STATES");
        hashtable.put("IN", "INDIA");

        System.out.println("hashtable : " + hashtable + "\n");

        /*
         * Returns a Set view of the keys contained in this map. The set is
         * backed by the map, so changes to the map are reflected in the set,
         * and vice-versa. 
         * 
         * If the map is modified while an iteration over the
         * set is in progress (except through the iterator's own remove
         * operation), the results of the iteration are undefined. The set
         * supports element removal, which removes the corresponding mapping
         * from the map, via the Iterator.remove, Set.remove, removeAll,
         * retainAll, and clear operations. It does not support the add or
         * addAll operations.
         */
        Set<String> set = hashtable.keySet();

        System.out.println("set : " + set + "\n");

        for( String key : set )
        {
            System.out.println(key);
        }
    }
}
Output
hashtable : {IN=INDIA, AF=AFGHANISTAN, BE=BELGIUM, US=UNITED STATES}

set : [IN, AF, BE, US]

IN
AF
BE
US
To Download HashtableDemoKeySet Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/HashtableDemoKeySet.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment