无法从 hashmap 的数组列表中删除重复元素
Cannot remove duplicate elements from arraylist of hashmap
我正在从内容提供商获取联系人,我需要在 listview.In 过程中显示它们 我成功获取联系人的过程,但它们包含 重复值 。现在Phone.CONTACT_ID 对于每个 contact.I 来说都是唯一的,希望根据该特定字段过滤我的联系人数组列表。
代码如下:
try {
cursor = getApplicationContext().getContentResolver()
.query(Phone.CONTENT_URI, null, null, null, null);
int Idx = cursor.getColumnIndex(Phone.CONTACT_ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
do {
System.out.println("=====>in while");
HashMap<String, String> hashMap = new HashMap<String, String>();
contactid=cursor.getString(Idx);
name = cursor.getString(nameIdx);
phoneNumber = cursor.getString(phoneNumberIdx);
image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
hashMapsArrayList.add(hashMap);
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
在这里你可以看到我正在从光标中获取 4 个字段并将它们绑定到数组列表 hashMapsArrayList
中,即 ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>();
。现在这个数组列表包含重复的字段,我想根据它进行过滤在 contactid.
上
代码如下:
System.out.println("Original--->"+hashMapsArrayList);
for(int i=0;i<hashMapsArrayList.size();i++)
{
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = hashMapsArrayList.get(i);
String contactiddup=resultp.get("contactid");
if(!( hashMapsArrayListRemovedup.contains(contactiddup)))
{
System.out.println(hashMapsArrayListRemovedup);
System.out.println("In if added");
hashMapsArrayListRemovedup.add(resultp);
}else{
System.out.println("In else");
}
}
但上面的代码不起作用,新的数组列表也包含重复值。
请帮忙
示例输出重复:
12-15 14:10:57.217: I/System.out(8971): [{name=Didi America, image=null, phoneNumber=, contactid=7996}, {name=Didi America, image=null, phoneNumber=, contactid=7996}]
您可以并且应该在构造列表时在 while 循环中删除重复项。只需使用 HashSet
来收集唯一 ID,并且只将具有新标识符的条目添加到列表中:
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
contactid=cursor.getString(Idx);
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
name = cursor.getString(nameIdx);
phoneNumber = cursor.getString(phoneNumberIdx);
image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
hashMapsArrayList.add(hashMap);
}
}
} while (cursor.moveToNext());
您不需要第二个代码片段(我没有检查它有什么问题)。
或者,您也可以检查删除频率。
参考这个 code.it 可能对你有帮助。
Collections.frequency(arrayList, number)
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(5);
arrayList.add(1);
arrayList.add(5);
arrayList.add(1);
arrayList.add(5);
arrayList.add(2);
arrayList.add(3);
ArrayList<Integer> unique = new ArrayList<>();
for (Integer number : arrayList) {
if (Collections.frequency(arrayList, number) == 1) {
unique.add(number);
}
}
System.out.println(unique);
}
使用HashSet
删除重复条目。
例如。
public static HashSet<String> hSet= new HashSet<String>();
使用 hSet.add(yourString);
向 HasSet 添加值
要从 HashSet
获取值,您必须使用 Iterator
Iterator iterator = hSet.iterator();
while(iterator.hasNext()){
String str = iterator.next();
}
我正在从内容提供商获取联系人,我需要在 listview.In 过程中显示它们 我成功获取联系人的过程,但它们包含 重复值 。现在Phone.CONTACT_ID 对于每个 contact.I 来说都是唯一的,希望根据该特定字段过滤我的联系人数组列表。
代码如下:
try {
cursor = getApplicationContext().getContentResolver()
.query(Phone.CONTENT_URI, null, null, null, null);
int Idx = cursor.getColumnIndex(Phone.CONTACT_ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_THUMBNAIL_URI);
cursor.moveToFirst();
do {
System.out.println("=====>in while");
HashMap<String, String> hashMap = new HashMap<String, String>();
contactid=cursor.getString(Idx);
name = cursor.getString(nameIdx);
phoneNumber = cursor.getString(phoneNumberIdx);
image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
hashMapsArrayList.add(hashMap);
}
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
在这里你可以看到我正在从光标中获取 4 个字段并将它们绑定到数组列表 hashMapsArrayList
中,即 ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>();
。现在这个数组列表包含重复的字段,我想根据它进行过滤在 contactid.
代码如下:
System.out.println("Original--->"+hashMapsArrayList);
for(int i=0;i<hashMapsArrayList.size();i++)
{
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = hashMapsArrayList.get(i);
String contactiddup=resultp.get("contactid");
if(!( hashMapsArrayListRemovedup.contains(contactiddup)))
{
System.out.println(hashMapsArrayListRemovedup);
System.out.println("In if added");
hashMapsArrayListRemovedup.add(resultp);
}else{
System.out.println("In else");
}
}
但上面的代码不起作用,新的数组列表也包含重复值。
请帮忙
示例输出重复:
12-15 14:10:57.217: I/System.out(8971): [{name=Didi America, image=null, phoneNumber=, contactid=7996}, {name=Didi America, image=null, phoneNumber=, contactid=7996}]
您可以并且应该在构造列表时在 while 循环中删除重复项。只需使用 HashSet
来收集唯一 ID,并且只将具有新标识符的条目添加到列表中:
Set<String> ids = new HashSet<>();
do {
System.out.println("=====>in while");
contactid=cursor.getString(Idx);
if (!ids.contains(contactid)) {
ids.add(contactid);
HashMap<String, String> hashMap = new HashMap<String, String>();
name = cursor.getString(nameIdx);
phoneNumber = cursor.getString(phoneNumberIdx);
image = cursor.getString(photoIdIdx);
System.out.println("Id--->"+contactid+"Name--->"+name);
if (!phoneNumber.contains("*")) {
hashMap.put("contactid", "" + contactid);
hashMap.put("name", "" + name);
hashMap.put("phoneNumber", "" + phoneNumber);
hashMap.put("image", "" + image);
// hashMap.put("email", ""+email);
hashMapsArrayList.add(hashMap);
}
}
} while (cursor.moveToNext());
您不需要第二个代码片段(我没有检查它有什么问题)。
或者,您也可以检查删除频率。 参考这个 code.it 可能对你有帮助。
Collections.frequency(arrayList, number)
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(5);
arrayList.add(1);
arrayList.add(5);
arrayList.add(1);
arrayList.add(5);
arrayList.add(2);
arrayList.add(3);
ArrayList<Integer> unique = new ArrayList<>();
for (Integer number : arrayList) {
if (Collections.frequency(arrayList, number) == 1) {
unique.add(number);
}
}
System.out.println(unique);
}
使用HashSet
删除重复条目。
例如。
public static HashSet<String> hSet= new HashSet<String>();
使用 hSet.add(yourString);
要从 HashSet
获取值,您必须使用 Iterator
Iterator iterator = hSet.iterator();
while(iterator.hasNext()){
String str = iterator.next();
}