无法从hashmap的arraylist中删除重复元素

时间:2015-12-15 08:36:30

标签: android arraylist contacts

我从内容提供商处获取联系人,我需要在列表视图中显示它们。在此过程中,我成功获取了联系人,但它们包含重复值。现在 Phone.CONTACT_ID < / strong>对于每个联系人都是唯一的。我想根据该特定字段过滤联系人的arraylist。

以下是代码:

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 ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>();。现在这个arraylist包含重复的字段,我想基于它来过滤它的的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");
        }

   }

但是上面的代码不起作用,新的arraylist也包含重复的值。

请帮忙。

示例输出重复:

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}]

3 个答案:

答案 0 :(得分:3)

在while循环中,您可以并且应该在构造List时删除重复项。只需使用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());

您不需要第二个代码段(我没有检查它是否有错误。)

答案 1 :(得分:1)

使用HashSet删除重复的条目。

EG。

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();
}

答案 2 :(得分:0)

或者,您也可以将频率检查为删除。 请参考此代码。它可以帮助您。

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);
}