title: Remove duplicate objects from a ArrayList in Android
body: <p>I know this has be discussed over and over again here, but none of the examples I've tried worked for me.</p>
  
  <p><strong>What I've got</strong></p>
  
  <p>I access the Call log from Android and I get a list of all calls made. Of course, here I get a lot of duplicates. 
  First I make a List</p>
  
  <pre><code>List&lt;ContactObject&gt; lstContacts = new ArrayList&lt;ContactObject&gt;();
  </code></pre>
  
  <p>Then I add objects into it</p>
  
  <pre><code>While (get some record in call log)
  {
      ContactObject contact = new ContactObject();
      contact.SetAllProperties(......)  
      lstContacts.add(contact);  
  }
  
  Set&lt;ContactObject&gt; unique = new LinkedHashSet&lt;ContactObject&gt;(lstContacts);
  lstContacts = new ArrayList&lt;ContactObject&gt;(unique);
  </code></pre>
  
  <p>The Contact Object class is simple</p>
  
  <pre><code>public class ContactObject {
  
      public ContactObject() {
          super();
      }
  
   @Override
   public boolean equals(Object obj) {
       if (!(obj instanceof ContactObject))
          return false;
  
       return this.lstPhones == ((ContactObject) obj).getLstPhones(); 
   }
  
   @Override
   public int hashCode() {
       return lstPhones.hashCode();
   }
  
      private long Id;
      private String name;
      private List&lt;String&gt; lstPhones;  
      private String details;
  
     //... getters and settres
  }
  </code></pre>
  
  <p><strong>What I need</strong></p>
  
  <p>I need to have a Contact only once in the list. As I've read around here there are a couple of things that can be done like Set, HashSet, TreeSet. TreeSet seems the best as it keeps the order just as I receive it from the Call log. I've tried to make my code work with it but no success. Could anyone be so kind to give me a sample code based on my example. Thank you for your time.</p>
  
  <p><strong>The Working Solution.</strong> Thank you all for your support, you've made my day.</p>
  
  <p>In ContactObject override the two methods</p>
  
  <pre><code> @Override
       public boolean equals(Object obj) {
           if (!(obj instanceof ContactObject))
              return false;
  
           return lstPhones.equals(((ContactObject) obj).getLstPhones());
       }
  
       @Override
       public int hashCode() {
           return (lstPhones == null) ? 0 : lstPhones.hashCode();
       }
  </code></pre>
  
  <p>//Getters and Setters and COnstructor....</p>
  
  <p>Simply use it as</p>
  
  <pre><code>Set&lt;ContactObject&gt; unique = new LinkedHashSet&lt;ContactObject&gt;(lstContacts);
  lstContacts = new ArrayList&lt;ContactObject&gt;(unique);
  </code></pre>