title: Comparing two list of objects in Java
body: <p>I have two list of Student Objects(listA &amp; listB) which were formed by querying from two different databases. I need to iterate one list and need to make sure it is not present in the other list.</p>
  
  <p>I have used the below comparison code for the same i.e overwritten the equals method and compared using for loops.</p>
  
  <p>Say List A &amp; List B could have 5000 rows each, can you suggest if there are better ways to implement this?</p>
  
  <p><strong>Comparison code:</strong></p>
  
  <pre><code>for (Student dataA:listA) {
      for (Student dataB:listB) {
          if(dataB.equals(dataA))
              break;                                              
      }
  }
  </code></pre>
  
  <p><strong>Student Object:</strong></p>
  
  <pre><code>public class Student {
      int A;
      int B;
      String C;   
  
      @Override
      public boolean equals(Object obj) {
          if (obj == this) {
              return true;
          }
          if (obj == null || obj.getClass() != this.getClass()) {
              return false;
          }
          Student student = (Student) obj;
          return A == student.A &amp;&amp; B == student.B &amp;&amp; C.equals(student.C);
      }
  }
  </code></pre>
  
  <p>Edit Note: ListA &amp; ListB can have different number of rows</p>