【问题标题】:Cannot remove duplicate elements from arraylist of hashmap无法从 hashmap 的数组列表中删除重复元素
【发布时间】:2015-12-15 08:36:30
【问题描述】:

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

代码如下:

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 个字段并将它们绑定到数组列表 hashMapsArrayListArrayList&lt;HashMap&lt;String, String&gt;&gt; hashMapsArrayList=new ArrayList&lt;HashMap&lt;String,String&gt;&gt;(); 。现在这个数组列表包含重复字段,我想根据 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}]

【问题讨论】:

  • 将 ArrayList 元素添加到 LinkedHashSet 以删除重复元素
  • 你的意思是什么重复值?提供者中没有重复数据
  • #kgandroid 请查看@Chirag 的回答

标签: android arraylist contacts


【解决方案1】:

您可以并且应该在构建 List 时在 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());

你不需要第二个代码 sn-p(我没有检查它有什么问题)。

【讨论】:

  • 出于好奇,您能否看看第二个代码 sn-p 并告诉我我做错了什么??
  • @kgandroid 看起来您只向hashMapsArrayListRemovedup 添加了唯一ID。您不会从 hashMapsArrayList 中删除重复项。
  • 是的,我正在这样做...我正在使用一个新的数组列表并将唯一元素复制到其中...但是它不起作用...所有值都添加到了新的数组列表中
  • @Eran 我已经尝试获得这个功能好几个星期了。在你的帮助下,终于结束了。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    或者,您也可以检查频率以及删除。 请参考此代码。它可能会对您有所帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 2012-05-09
      • 1970-01-01
      • 2018-01-01
      • 2021-06-21
      • 1970-01-01
      • 2013-05-20
      相关资源
      最近更新 更多