title: Java: Most efficient way to find matching object values in two ArrayLists?
body: <p>I have two ArrayLists of objects: <code>modelList</code> which contains <code>Model</code> objects, and <code>entityList</code> which contains <code>Entity</code> objects. Both <code>Model</code> and <code>Entity</code> objects have a property called <code>id</code>.</p>
  
  <p>My goal is to loop through each <code>Model</code> in <code>modelList</code> and, if there is an <code>Entity</code> in <code>entityList</code> with the same <code>id</code> value, call the method <code>merge()</code>.</p>
  
  <p>Currently, I am doing this:</p>
  
  <pre><code>for (Model model : modelList) {
      for (Entity entity : entityList) {
          if (model.getId().equals(entity.getId())) merge(entity, model);
      }
  }
  </code></pre>
  
  <p>This doesn't seem very efficient, especially with a large dataset. What would be a better way of achieving the desired result?</p>
  
  <p>Thanks in advance!</p>