title: Find the difference between two Arrays
body: <p>this does not look like a duplicate to existing intersection / union questions (I may wrong).</p>
  
  <p>I have two ArrayLists, which contains a class that contains a value, e.g.,</p>
  
  <pre><code>class A {
      private int value;
  
      public int getValue() {
          return value;
      }
  }
  </code></pre>
  
  <p>And my Lists are</p>
  
  <pre><code>ArrayList&lt;A&gt; first, second;
  </code></pre>
  
  <p>I want to find two indices from <code>first</code> and <code>second</code>, which first indices are points to items in <code>first</code> that the respective values only in <code>first</code> but not in <code>second</code>, and the other indices are indices in <code>second</code> that the respective values only in <code>second</code> but not in <code>first</code>.</p>
  
  <p>For example (read from center rows, where has the data):</p>
  
  <pre><code>hit                  *   *
  index of first   0   1   2   3
  ----------------------------------
  first.value      1   3   5   7
  second.value     1 2   4   6 7 8
  ----------------------------------
  index of second  0 1   2   3 4 5
  hit                *   *   *   *
  </code></pre>
  
  <p>I want two indices of <code>1 2</code> and <code>1 2 3 5</code>.</p>
  
  <p>Note:</p>
  
  <ol>
  <li>I prefer not to use 3rd party libraries</li>
  <li>Values are integers and sorted, and I the return must also in order.</li>
  <li>Lengths of <code>first</code> and <code>second</code> are uncertain - either one can be longer than the other.</li>
  </ol>
  
  <p>Thanks.</p>
  
  <p>PS: Anyone need more detail, this is a simplified version of my problem. <code>first</code> is actually a ResultSet of SQL query, which are all records on the server, <code>second</code> is a list of local record (or consider processed from another local database), what I want to do is to delete all records on the server (in <code>first</code>) that does not contained in the local database (<code>second</code>), and add those missing record to remote database. Thanks.</p>
  
  <hr>
  
  <p>To @Tibrogargan</p>
  
  <p>My target is to incrementally transfer my local database (sqlite) to remote database (MySQL). The local databases has a field called <code>LocalId</code> (no duplicate and not the primary key) and of course other contents (~10 fields), and apart from copy required fields (~5 fields) to the remote database, I also included a field to determine if the record is deleted from local database.</p>
  
  <p>As other requirement, I have already read all local data into a <code>ArrayList</code>, sorted by <code>LocalId</code> on local database, and have access to all remote data via <code>rs</code>, sorted by <code>LocalId</code> on remote database.</p>
  
  <p>My earlier version of code was to find the last <code>LocalId</code> on local database, and mark all records on remote database where their <code>LocalId</code> is greater than the max <code>LocalId</code> on local database. However, it was correct until I found I don't always delete the tail records on local database, so I found it is necessary to iterate all messages and compare the existence.</p>
  
  <p>I don't know if there're any "better" solution but seems that if I load all local messages to remote, the performance will be horrible?</p>
  
  <p>Oh, just need to mention, there are only tens of messages need to be deleted and added to remote, out from hundreds to 100+k records existing on both remote and local dataset. The logic of determine if a record on remote should be marked as deleted is rely on if the respective message (same <code>LocalId</code>) exists on local dataset.</p>