简体   繁体   中英

android Java hashtable failed to store some characters

I define a hashtable as below:

Hashhtable<String, String> dicts = new Hashtable<String, String>();
dicts.put("天", "sky");  
String hashstr =(String)dicts.get("天");

However, the return value is null.

I tried with other words, it is ok. But the word "天" cannot be stored, I wonder why? thanks in advance.

You simply have different strings. If you copy paste from one string to the other one, it works :

Hashtable<String, String> dicts = new Hashtable<>(); // no need to repeat the whole type
dicts.put("天", "sky");  
String hashstr = dicts.get("天"); // no need to cast here
System.out.println(hashstr); // prints "sky"

Be careful that Unicode is full of things your editor may not render. More precisely, you have an invisible char at the start of your string (in the call to put ). An easy way to spot the problem without resorting to an hex editor is to move the cursor in any text editor : it will stay in place at the first hit on the right arrow.

There is no issue with putting this character to Hashtable . Take look at following

    Hashtable<String, String> dicts = new Hashtable<String, String>();
    dicts.put("天", "sky");
    System.out.println(dicts.size());

This will print 1 . So now we can see it is storing

May be issue with some thing else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM