Michael
Michael

Reputation: 439

How to remove duplicated contacts from ArrayList

i have a huge problem with duplicated contacts. After sorting array with:

Collections.sort(mAllContacts);

I'm reading contacts with :

ContentResolver cr = mContext.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if ((cur != null ? cur.getCount() : 0) > 0) {
            while (cur != null && cur.moveToNext()) {
                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));

                if (cur.getInt(cur.getColumnIndex(
                        ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contacts = new AllContacts(name, phoneNo);
                        mAllContacts.add(contacts);
                    }
                    pCur.close();
                }
            }
        }
        if (cur != null) {
            cur.close();
        }

With this way, all contact are retrieved to a list (Local storage, Sim, Gmail etc). I have no problem to remove duplicated contact by name like this:

for (int i = 0; i < mAllContacts.size() - 1; i++) {
        if (mAllContacts.get(i).getmContactName().equals(mAllContacts.get(i + 1).getmContactName())) {
            Log.d("duplicatedArray", "setAdapter: " + mAllContacts.get(i).getmContactName());
            mAllContacts.remove(i+1);
        }
    }

but it's not a good practice because some times different contacts may have same name, so i can remove duplicated contact with same method but use:

mAllContacts.get(i + 1).getmPhoneNumber()

And here is the problems comes: For some reason , the phone number have different format while reading from gmail, local storage , sim. For ex.

Gmail phone number : +972-54-333-55-88
Local storage phone number : +972-543335588
Sim : +972543335588

How can i solve my problem to remove duplicated values. And yes , i need to read contact from all places where they appear (gmail, local storage , sim)

Upvotes: 1

Views: 201

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

Apply regex to change the same phone numbers to a common format e.g.

class Main {
    public static void main(String args[]) {
        String p1 = "+972-54-333-55-88";
        String p2 = "+972-543335588";
        String p3 = "+972543335588";
        String p4 = "+97(2543)335588";
        String p5 = "+97 2543 335588";
        String regex = "[^0-9+]";

        System.out.println(p1.replaceAll(regex, ""));
        System.out.println(p2.replaceAll(regex, ""));
        System.out.println(p3.replaceAll(regex, ""));
        System.out.println(p4.replaceAll(regex, ""));
        System.out.println(p5.replaceAll(regex, ""));
    }
}

Output:

+972543335588
+972543335588
+972543335588
+972543335588
+972543335588

Upvotes: 5

rhowell
rhowell

Reputation: 1187

Normalize

I think you should normalize these phone numbers before you store them. So for example all of these:

Gmail phone number : +972-54-333-55-88
Local storage phone number : +972-543335588
Sim : +97254335588

Could just be stored as 97254335588 You can parse these numbers by removing all space, hyphens, and plus signs.

HashSet

It would also be easier to store these in a HashSet so you don't even need to worry about removing duplicates.

Upvotes: 2

Related Questions