I am working with Hashtable in my java program. I just surprised by seeing the abnormal behavior of Hastable. Below is my code (This is not my final code, i simply created a new simple project with the code which is running abnormal)
Hashtable<char[], char[]> h1 = new Hashtable<char[], char[]>();
char[] key = Integer.toString(12).toCharArray();
char[] val = Integer.toString(21).toCharArray();
h1.put(key, val);
System.out.println(h1.containsKey(Integer.toString(12).toCharArray()));// Should print true, since 12 is there in Hashtable
You can’t use arrays like this as map keys, because arrays have the default, referential-equality-based
Objectimplementations ofequalsandhashCode. UsingStringas the key instead would make your program work as desired.