Tuesday 3 March 2015

Java : Collection Framework : Hashtable (Keys)


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

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

/*
 * Example of keys() 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 an enumeration of the keys in this hashtable.
         */
        Enumeration<String> enumeration = hashtable.keys();

        
        System.out.println("key");
        System.out.println("-----");
        
        /*
         * Tests if this enumeration contains more elements.
         */
        while( enumeration.hasMoreElements() )
        {
            /*
             * Returns the next element of this enumeration if this enumeration
             * object has at least one more element to provide.
             */
            String key = enumeration.nextElement();
            System.out.println(key);

        }

    }
}
Output
hashtable : {IN=INDIA, AF=AFGHANISTAN, BE=BELGIUM, US=UNITED STATES}

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

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

    1. Hai ram, really it is very good.. i have some doubt. Can u explain what is the purpose of hashtable? actually with hashMap also we done same functionality better than this.

      ReplyDelete
    2. Thanks for the feedback. Yes you are correct using HashMap also we can achieve same functionality and we should use HashMap only and should not use Hashtable.Hashtable is old implementation and it is synchronized.

      ReplyDelete