Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}]
like image 976
kgandroid Avatar asked Sep 26 '22 22:09

kgandroid


2 Answers

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).

like image 103
Eran Avatar answered Oct 03 '22 19:10

Eran


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();
}
like image 24
Chirag Savsani Avatar answered Oct 03 '22 17:10

Chirag Savsani