Copying Values Between HashMap and Hashtable
By admin on Aug 4, 2008 in Java, Programming
What is the problem with the following code?
String userName = null;
if (......) {
userName = <some value>;
}
Map<String,String> hm = new HashMap<String,String>();
hm.put("username", userName);
.....
Map<String,String> ht = new Hashtable<String,String)(hm);
There is a bug in this code. What the the userName remains NULL when you are copying the value to the Hashtable. Hashtable does not allow NULL as part of the value, and depending on the platform and JDK that you are using, you may or may not see any errors at all, and the program will just exit by itself.
Post a Comment