kgandroid
kgandroid

Reputation: 5595

Cannot remove duplicate elements from arraylist of hashmap

I am fetching contacts from content provider and I need to display them in a listview.In the process I successfully fetch the contacts,but they contains duplicate values.Now Phone.CONTACT_ID is unique for each contact.I want to filter my arraylist of contacts based on that particular field.

Here is the code:

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

Here you can see that I am fetching 4 fields from the cursor and binding them into the arraylist hashMapsArrayList which is ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); .Now this arraylist contains duplicate fields and I want to filter it based on the contactid.

Here is the code:

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

   }

But the above code is not working and the new arraylist also contains duplicate values.

Please help.

A sample output duplicacy:

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

Upvotes: 5

Views: 1327

Answers (3)

Chirag Savsani
Chirag Savsani

Reputation: 6140

Use HashSet to remove duplicate entries.

Eg.

public static HashSet<String> hSet= new HashSet<String>();

Add value to HasSet using hSet.add(yourString);

For get value from HashSet you have to use Iterator

Iterator iterator = hSet.iterator(); 
while(iterator.hasNext()){
        String str = iterator.next();
}

Upvotes: 1

JIGAR
JIGAR

Reputation: 302

Alternatively you can check for frequency as remove as well. refer this code.it may help you.

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

Upvotes: 0

Eran
Eran

Reputation: 393821

You can, and should remove the duplicates when you construct the List, in the while loop. Just use a HashSet to collect the unique ids, and only add entries having new a identifier to the List :

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

You don't need the second code snippet (I didn't check to see what's wrong in it).

Upvotes: 3

Related Questions