how to remove duplicate contacts from arraylist

前端 未结 5 1528
谎友^
谎友^ 2021-01-16 21:13

I have created an app in which I am getting the contacts from a device. But I want to remove the duplicate contacts from the results.

How could I do it?

相关标签:
5条回答
  • 2021-01-16 21:42

    use below code list=removeDuplicates(list);

    public List<ProfileBean> removeDuplicates(List<ProfileBean> list) {
        // Set set1 = new LinkedHashSet(list);
        Set set = new TreeSet(new Comparator() {
    
            @Override
            public int compare(Object o1, Object o2) {
                if (((ProfileBean) o1).getName().equalsIgnoreCase(((ProfileBean) o2).getName()) &&
                        ((ProfileBean)o1).getPhoneNumber().equalsIgnoreCase(((ProfileBean)o2).getPhoneNumber())) {
                    return 0;
                }
                return 1;
            }
        });
        set.addAll(list);
    
        final List newList = new ArrayList(set);
        return newList;
    }
    
    0 讨论(0)
  • 2021-01-16 21:43
    • If you want to get rid of duplicates, consider using a HashSet instead.

    • If you can't/don't want to use it, simply check before adding whether the contact is already there.

      if (!myList.contains(newContact))
        myList.add(newContact);
      
    0 讨论(0)
  • 2021-01-16 21:46

    The following function can be used for removing duplicates from String ArrayList Change it according to your requirement

    public ArrayList<String> listWithoutDuplicates(ArrayList<String> duplicateList) {
    
        // Converting ArrayList to HashSet to remove duplicates
        LinkedHashSet<String> listToSet = new LinkedHashSet<String>(duplicateList);
    
        // Creating Arraylist without duplicate values
        ArrayList<String> listWithoutDuplicates = new ArrayList<String>(listToSet);
    
        return listWithoutDuplicates;
    }
    
    0 讨论(0)
  • 2021-01-16 22:01

    Try to get ContactsContract.Contacts.NAME_RAW_CONTACT_ID) its unique id and its used for update contacts compare your contacts with raw id is same or not as below

        private void getAllContactsBackground() {
    
        ContentResolver contentResolver = getActivity().getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    
    
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor cursorInfo = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                    InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getActivity().getContentResolver(),
                            ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id)));
    
                    Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id));
                    Uri pURI = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    
    
                    Bitmap photo = null;
                    if (inputStream != null) {
                        photo = BitmapFactory.decodeStream(inputStream);
                    }
    
    
                    while (cursorInfo.moveToNext()) {
                        ContactsModel info = new ContactsModel();
                        info.contacts_id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        info.contacts_raw_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
                        info.contacts_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        info.contacts_mobile = cursorInfo.getString(cursorInfo.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceFirst("[^0-9]" + "91", "");
                        info.contacts_photo = photo;
                        info.contacts_photoURI = String.valueOf(pURI);
    
    
                        Cursor emailCur = contentResolver.query(
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                        while (emailCur.moveToNext()) {
    
                            info.contacts_email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            Log.e("email==>", "" + info.contacts_email);
    
                        }
                        emailCur.close();
    
    
                        int flag = 0;
                        if (arrayListAllContacts.size() == 0) {
                            arrayListAllContacts.add(info);
                        }
                        for (int i = 0; i < arrayListAllContacts.size(); i++) {
    
                            if (!arrayListAllContacts.get(i).getContacts_raw_id().trim().equals(info.contacts_raw_id)) {
                                flag = 1;
    
                            } else {
                                flag = 0;
                                break;
                            }
    
                        }
                        if (flag == 1) {
                            arrayListAllContacts.add(info);
                        }
    
    
                    }
                    cursorInfo.close();
    
                }
            }
            cursor.close();
    
        }
    }
    
    0 讨论(0)
  • 2021-01-16 22:02

    Add the following checking in the place of list.add(new ProfileBean(name, phoneNumber)); before adding into list:

    int flag = 0
    if(list.size() == 0){
    list.add(new ProfileBean(name, phoneNumber));
    }
        for(int i=0;i<list.size();i++){
    
        if(!list.get(i).getProfileName().trim().equals(name)){
        flag = 1;
    
        }else{
         flag =0;
         break;
    

    }

        }
    if(flag == 1){
    list.add(new ProfileBean(name, phoneNumber));
    }
    
    0 讨论(0)
提交回复
热议问题