title: Remove duplicates Objects from List in Java
body: <p>I know this kind of question is asked in stackoverflow lots of time before. But my issue is little bit different and I could not find any similar scenario, so posting this question here</p>
  
  <p>Problem:
  I need to remove duplicate objects from ArrayList. Structure of my arrayList is as below</p>
  
  <pre><code>dataList.add(new ObjectClass("a","b"));
  dataList.add(new ObjectClass("c","n"));
  dataList.add(new ObjectClass("b","a")); // should be counted as duplicate
  dataList.add(new ObjectClass("z","x"));
  </code></pre>
  
  <p>I need to remove objects from above list such as, it treats combination of "a,b" and "b,a" as duplicates and remove any of those duplicate</p>
  
  <p>My solution:
  step 1) Override equals method in DataClass class</p>
  
  <pre><code>class DataClass {
        String source;
        String destination;
  
        DataClass(String src, String dest) {
          this.source = src;
          this.destination = dest;
        }
  
        // getter setter for source and destination variables
  
      @Override
      public boolean equals(Object obj) {
          System.out.println("inside equals");
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          ObjectClass other = (ObjectClass) obj;
          if(i.equals(other.getJ())
              &amp;&amp; j.equals(other.getI())) {
              return true;
          } else return false;
      }
  </code></pre>
  
  <p>step 2) method to remove duplicates</p>
  
  <pre><code>public List&lt;DataClass&gt; removeDuplicates(List&lt;DataClass&gt; dataList) {
              List&lt;DataClass&gt; resultList = new ArrayList&lt;DataClass&gt;();
  
              // Convert array list to Linked list
              LinkedList&lt;DataClass&gt; linkedList = new LinkedList&lt;DataClass&gt;();
              for(DataClass obj: dataList) {
                  linkedList.add(obj);
              }
  
              // Iterate through linked list and remove if values are duplicates
              for(int i = 0; i&lt;linkedList.size();i++) {
                for(int j = i+1;j&lt;linkedList.size();j++) {
                  if(linkedList.get(j).equals(linkedList.get(i))) {
                      linkedList.remove();
                  }
                }
              }
              resultList.addAll(linkedList);
              return resultList;
          }
  </code></pre>
  
  <p>I am still looking for any better optimized solution, if there is any. Thanks in advance</p>
  
  <p><strong>Update with solution</strong>
  : my equals method was needed to correct some comparison logic. So here is my updated ObjectClass instead of DataClass including correct overridden equals method</p>
  
  <pre><code>public class ObjectClass {
      String i;
      String j;
      public  ObjectClass(String i, String j) {
          this.i = i;
          this.j = j;
      }
      // getters setters
      // override hashcode
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          ObjectClass other = (ObjectClass) obj;
  
          if((i.equals(other.getJ()) || i.equals(other.getI()))
              &amp;&amp; (j.equals(other.getI()) || j.equals(other.getJ()))) {
              return true;
          } else return false;
      }   
  }
  </code></pre>
  
  <p>2) After fixing equals method I tried below implementation in removeDuplicate method as Janos mentioned and it is working fine as expected</p>
  
  <pre><code>    for(ObjectClass obj: dataList) {
          if(!resultList.contains(obj))
              resultList.add(obj);
      }
  </code></pre>
  
  <p>Thank you very much</p>