title: Java Compare two lists and create two list one found and another one not found
body: <p>I want to compare two lists in java and build two lists oneFound and another one NotFound.</p>
  
  <p>Here's my code so far, can you please advice for the best results and efficient way to do it.</p>
  
  <p>So bascially i am getting a list of emails to be added or update. Now what i am try to achieve is I want to check those emails against the ones in the DB. if they exist in DB then simply update them otherwise add them.</p>
  
  <p>For that I get the list of emails from the DB and then try to compare that list with the list of emails which are to be added or updated.</p>
  
  <p>Then I build 2 lists one Found and another one not found. The foundones are updated and not found ones are which will be inserted into DB.</p>
  
  <p>The following code builds not found with the lists which are already in db resulting in the DB insertion (causing duplicate records).</p>
  
  <p>So if I correctly build found and notfound then the update and insertion will be working correctly.</p>
  
  <pre><code>public updateData (List&lt;String&gt; emailToAddList, List&lt;String&gt; emailToDeleteList) 
  {
  
      List&lt;String&gt; emailsFromDB = Service.getEmailsFromDB();
      List&lt;String&gt; emailToUpdateFound = new ArrayList&lt;String&gt;( );
      List&lt;String&gt; emailToUpdateNotFound = new ArrayList&lt;String&gt;();
  
      /**
      **  compare emailToAddList with emailsFromDB, if found populate 
      **  emailToUpdateFound for data update, otherwise populate emailToUpdateNotFound for data insetion
      **/
  
      Collections.sort(emailListToAdd);
      Collections.sort(emailListfromDB);
  
      if(emailListToAdd.size() &gt; emailListfromDB.size()
      {
      for(String addStr: emailListToAdd)
      {
          if(emailListfromDB.contains(addStr))
          {
              emailToUpdateFound.add(addStr);
          }
          else
          {
              emailToUpdateNotFound.add(addStr);
          }
      }
  
      }
      else
      { 
          for(String str: emailListfromDB)
          {
              if(emailListToAdd.contains(str))
              {
                  emailToUpdateFound.add(str);
              }
              else
              {
                  emailToUpdateNotFound.add(str);
              }
  
          }
      }
  }
  </code></pre>
  
  <p>Thanks</p>