Java HashMap的containsKey方法返回false,但它是否具有该键?

Java HashMap的containsKey方法返回false,但它是否具有该键?,java,android,Java,Android,这是我的地图记录。存在具有两个相同密钥的。这怎么可能 Map<String, Objects> map = new HashMap<String, Objects>(); addContact("+917111111111"); addContact("+919222222222"); addContact("+919222222222"); private void addContact(String number){ if(TextUti

这是我的地图记录。存在具有两个相同密钥的。这怎么可能

 Map<String, Objects> map = new HashMap<String, Objects>();

 addContact("+917111111111");
 addContact("+919222222222");
 addContact("+919222222222");

 private void addContact(String number){
        if(TextUtils.isEmpty(number))return;

        number = number.trim();
        number = number.replaceAll("-", "");
        number = number.replaceAll(" ", "");

        if(!map.containsKey(number)) {
            map.put(number, null);
        }
    }
    /* While debugging in android studio. I have found the map have below entry.
        0 = {HashMap$HashMapEntry@3798} "+919222222222" -> "null"
        1 = {HashMap$HashMapEntry@3832} "‪+919222222222" -> "null"
        2 = {HashMap$HashMapEntry@3694} "+917111111111" -> "null"
    */
    map.containsKey("+919222222222");// ==> return false
Map Map=newhashmap();
添加联系人(“+917111111”);
添加联系人(“+91922222”);
添加联系人(“+91922222”);
专用void addContact(字符串编号){
if(TextUtils.isEmpty(number))返回;
number=number.trim();
number=number.replaceAll(“-”和“”);
number=number.replaceAll(“,”);
如果(!map.containsKey(编号)){
map.put(数字,空);
}
}
/*在android studio中调试时。我发现地图上有下面的条目。
0={HashMap$HashMapEntry@3798}“+91922222”->“空”
1={HashMap$HashMapEntry@3832} "‪+91922222“->“空”
2={HashMap$HashMapEntry@3694}“+917111111”->“空”
*/
map.containsKey(“+91922222”);/=>返回错误
为什么会发生这种情况

实际任务:

private void getContacts(){
    try {
        Cursor cursor = null;
        StringBuffer sb = new StringBuffer();

        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String strOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, strOrder);
            int contactIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
            int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID);
            cursor.moveToFirst();
            do {
                String idContact = cursor.getString(contactIdIdx);
                String name = cursor.getString(nameIdx);
                String phoneNumber = cursor.getString(phoneNumberIdx);
                //...
                phoneNumber = getFormatedNumber(phoneNumber);

                //as map key same phone number can not be two times
                if(!map.containsKey(phoneNumber)) {
                    map.put(phoneNumber, null);
                    sb.append("\nPhone Number:--- " + phoneNumber + "\nUser Name:--- "
                            + name);
                    sb.append("\n----------------------------------");
                }
            } while (cursor.moveToNext());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        textView.setText(sb); //As in output it shows one number showing two times
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 private String getFormatedNumber(String number){
        if(TextUtils.isEmpty(number))return null;

        number = number.trim();
        number = number.replaceAll("-", "");
        number = number.replaceAll(" ", "");

        return number;
    }
private void getContacts(){
试一试{
游标=空;
StringBuffer sb=新的StringBuffer();
Map Map=newhashmap();
试一试{
String strOrder=contacts contract.commondatatypes.Phone.DISPLAY_NAME+“ASC”;
cursor=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null,strOrder);
int contactIdIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.\u ID);
int nameIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.DISPLAY\u NAME);
int phoneNumberIdx=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER);
int photoidix=cursor.getColumnIndex(ContactsContract.CommonDataTypes.Phone.PHOTO_ID);
cursor.moveToFirst();
做{
String idContact=cursor.getString(contactIdIdx);
String name=cursor.getString(nameIdx);
String phoneNumber=cursor.getString(phoneNumberIdx);
//...
phoneNumber=GetFormattedNumber(phoneNumber);
//由于地图键相同,电话号码不能重复两次
如果(!map.containsKey(电话号码)){
map.put(电话号码,空);
sb.追加(“\n电话号码:--”+电话号码+”\n用户名:--”
+姓名);
sb.追加(“\n---------------------------------------------------”);
}
}while(cursor.moveToNext());
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(光标!=null){
cursor.close();
}
}
textView.setText(sb);//在输出中,它显示一个数字,显示两次
}捕获(例外e){
e、 printStackTrace();
}
}
私有字符串GetFormattedNumber(字符串编号){
if(TextUtils.isEmpty(number))返回null;
number=number.trim();
number=number.replaceAll(“-”和“”);
number=number.replaceAll(“,”);
返回号码;
}

经过所有讨论后,我发现问题是由于在调试时字符串中添加了不可见的unicode字符导致的,但如果我们复制到记事本中,它就会清晰可见。如下所示:
“\u202A\u202A+91922222222\u202A\u202C”

经过所有讨论后,我发现问题是由于字符串中的unicode字符追加导致的,该字符在调试时是不可见的,但如果复制到记事本中,则它清晰可见。如下所示:
“\u202A\u202A+91922222222\u202A\u202C”

你能显示更多的代码,包括地图填充和日志记录吗?你怎么会有两个相同键的条目?
{+917111111111=null,+91922222222=null}
我得到了这样的结果。我不知道你是如何使用这段代码的,但我把你的代码放到了一个快速类中,最后,map包含
{+917111111=null,+91922222=null}
和containsKey返回
true
@thinh.lam
java.lang.String
是最终类,您无法修改它,它的
hashCode()
函数工作正常。此外,您不能像这样实例化自己的类
“hello”
。唯一的解释是不同的JDK,它提出了关于Android和这些类到底是什么的问题。你能展示更多的代码,包括地图填充和日志记录吗?你如何有两个相同密钥的条目?
{+917111111=null,+91922222=null}
true
我得到了这样的结果。我不知道你是如何使用这段代码的,但我把你的代码放到了一个快速类中,最后,map包含
{+917111111=null,+91922222=null}
和containsKey返回
true
@thinh.lam
java.lang.String
是最终的类,您无法修改它,它的
hashCode()
函数工作正常。此外,您不能像这样实例化自己的类
“hello”
。唯一的解释是不同的JDK,它提出了关于Android以及这些类到底是什么的问题。