java - Hashtable of type <String,String> is storing <String, Integer> type value as I see in the log cat and debugger. Is it possible? -


this first question posting here. since guys have answered many question , find answers, stackoverflow. being specific problem- have static hastable type <string,string>. when setting values , key other activity, in specific case(i not sure of case) hastable having data of type <string, integer>. checked in debugger showing same. mistake have made can please me pointing issues? thanks.

edit code-

       hashtable<string,string> details;         details= new hashtable<>();         hashtable temp;         temp = details;         temp.put("key1", new integer("1"));         temp.put("key2", "1");         log.d("details", details.get("key1"));//causes classcastexception: java.lang.integer cannot cast java.lang.string         log.d("temp",temp.get("key1").tostring());//no issue ,worked 

can hashtable of type <string,string> contains value of type <string, integer>

yes possible. actual runtime typing hashtable uses object key , value types. (for longer explanation google "java type erasure" ...)

for example, when write this:

 hashtable<string, string> tab = new hashtable<>();  tab.put("a", "b");  string res = tab.get("a"); 

the last statement doing under covers:

 string res = (string) tab.get("a"); 

if have played rules, typecast can never fail.

what mistake have made can please me pointing issues?

without seeing code can guess, 1 possible explanation have ignored (or suppressed) critical "unchecked conversion" warning or similar. example:

 hashtable<string, integer> tab = new hashtable<>();  tab.put("a", new integer(42));   // unchecked conversion  hashtable<string, string> bad = (hashtable<string, string>) tab;   string res = bad.get("a");    // ooops! 

you should never ignore compiler warnings. safe suppress them, need understand mean make judgement. if suppress them without due consideration, liable write buggy code throws classcastexception in unexpected places.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -