title: Compare 2 Java arraylists of different objects and add the matching rows to a new List
body: <p>We need to compare 2 arraylists of different objects having some common fields, and then store the matching rows to a new arraylist. I have searched for solutions, but wasn't able to get what I need. </p>
  
  <pre><code>List&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();
  Person:
  private String firstName;
      private String lastName;
      private String street1;
      private String street2;
      private String city;
      private String stateCode;
      private String zipCode;
  
  List&lt;PersonNpi&gt; npiList = new ArrayList&lt;PersonNpi&gt;();
  PersonNpi:
  private String name;
      private String npi;
      private Address address;
  </code></pre>
  
  <p>So I need to check if the <code>name &amp; address in the PersonNpi object in the PersonNpiList match to a Person object in the PersonList</code>, and if yes save the <code>Person details + Npi to a new Arraylist&lt;Employee&gt;</code></p>
  
  <p>Hope I'm clear on the question. Please let me know on how to solve this efficiently.</p>
  
  <p>Thanks</p>
  
  <p>Harry </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><em>I need to save the non-matching rows (on the first arraylist) as well to another list. Do I need to have another loop or can I do it on the same For loop? Anyone please?</em></p>