title: Cannot remove duplicate elements from arraylist of hashmap
body: <p>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 <strong>duplicate values</strong>.Now <strong>Phone.CONTACT_ID</strong> is unique for each contact.I want to filter my arraylist of contacts based on that particular field.</p>
  
  <p>Here is the code:</p>
  
  <pre><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("=====&gt;in while");
                  HashMap&lt;String, String&gt; hashMap = new HashMap&lt;String, String&gt;();
  
                  contactid=cursor.getString(Idx);
                  name = cursor.getString(nameIdx);
                  phoneNumber = cursor.getString(phoneNumberIdx);
                  image = cursor.getString(photoIdIdx);
                  System.out.println("Id---&gt;"+contactid+"Name---&gt;"+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();
              }
          }
  </code></pre>
  
  <p>Here you can see that I am fetching 4 fields from the cursor and binding them into the arraylist  <code>hashMapsArrayList</code> which is   <code>ArrayList&lt;HashMap&lt;String, String&gt;&gt; hashMapsArrayList=new ArrayList&lt;HashMap&lt;String,String&gt;&gt;();</code> .Now this arraylist contains duplicate fields and I want to filter it based on the <strong>contactid</strong>.</p>
  
  <p>Here is the code:</p>
  
  <pre><code> System.out.println("Original---&gt;"+hashMapsArrayList);
     for(int i=0;i&lt;hashMapsArrayList.size();i++)
     {
          HashMap&lt;String, String&gt; resultp = new HashMap&lt;String, String&gt;();
          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");
          }
  
     }
  </code></pre>
  
  <p>But the above code is not working and the new arraylist also contains duplicate values.</p>
  
  <p>Please help.</p>
  
  <p>A sample output duplicacy:</p>
  
  <pre><code>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}]
  </code></pre>