title: How to compare a field between two Lists of objects?
body: <p>Let's suppose I've an object that looks like this:</p>
  
  <pre><code>public class Supermarket {
  
      public String supermarketId;
      public String lastItemBoughtId;
  
      // ...
  }
  </code></pre>
  
  <p>and I have two lists of supermarkets, one "old", another "new" (i.e. one is local, the other is retrieved from the cloud).</p>
  
  <pre><code>List&lt;Supermarket&gt; local = getFromLocal();
  List&lt;Supermarket&gt; cloud = getFromCloud();
  </code></pre>
  
  <p>I would like to find all the pairs of <code>Supermarket</code> objects (given <code>supermarketId</code>) that have <code>lastItemBoughtId</code> different from one another.</p>
  
  <p>The first solution I have in mind is iterating the first <code>List</code>, then inside the first iteration iterating the second one, and each time that <code>local.get(i).supermarketId.equals(cloud.get(j).supermarketId)</code>, checking if <code>lastItemBoughtId</code> of the <code>i</code> element is different from the id of the <code>j</code> element. If it's different, I add the whole <code>Supermarket</code> object on a new list.</p>
  
  <p>To be clearer, something like this:</p>
  
  <pre><code>List&lt;Supermarket&gt; difference = new ArrayList&lt;&gt;();
  for (Supermarket localSupermarket : local) {
      for (Supermarket cloudSupermarket : cloud) {
          if (localSupermarket.supermarketId.equals(cloudSupermarket.supermarketId) &amp;&amp;
          !localSupermarket.lastItemBoughtId.equals(cloudSupermarket.lastItemBoughtId))
              difference.add(cloudSupermarket);
      }
  }
  </code></pre>
  
  <p>Clearly this looks <em>greatly</em> inefficient. Is there a better way to handle such a situation?</p>