这是我的地图条目的日志。那有两个相同的键存在。这怎么可能?
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
为什么会这样呢?
实际任务:
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;
}
最佳答案
经过所有讨论,我发现类似问题的问题是由于在调试过程中看不见字符串中的Unicode字符附加而引起的,但是如果我们将其复制到记事本中,则该问题清晰可见。像:
'\ u202A \ u202A + 91922222222 \ u202A \ u202C'