free web hosting | free website | Web Hosting | Free Website Submission | shopping cart | Coaching Institute | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
Hashtable in JDK 1.5.0

Hashtable in JDK 1.5.0

In order to run Java programs on the new JDK version 1.5 'Tiger' virtual machine written for JDK 1.4.2 or any other previous JDK package, compile the program with -Xlint:unchecked option.

   javac -Xlint:deprecation -Xlint:unchecked *.java

The compiled program will run on the new 1.5 vertual machine as is. If the program uses Hashtable, though, the compiler will give off some warning like this.

   warning: [unchecked] unchecked call to put(K,V) 
     as a member of the raw type java.util.Hashtable

In this case, specify the types for the key and value. In the following case, the key and value are typed as JMenuItem and Integer objects, respectively.

   private Hashtable<JMenuItem, Integer> comp= 
                 new Hashtable<JMenuItem, Integer>();

By specifying the types for the Hashtable class, the program can check the types for the key and value at run time. This means, there is no need to cast the generic Object class object every time you call get(key, value) function.

	Integer n=(Integer)comp.get(source);

   -->

	Integer n=comp.get(source);

It is even possible to 'nest' the type specification. If the elements of a hashtable have the type hashtable, specify the element type with inequal signs as usual.

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

Finally, compile the program without the -Xlint:unchecked option and the warnings should dissapear.


   javac -Xlint:deprecation *.java

Now, the program is safely converted for the new virtual machine. Happy programming!


Return to Home Page
Erica Asai
Last Modifed: Sun Jun 15 22:28:34 2003