title: How to stream and map a one-to-many relationship?
body: <p>Say I have the following 2 objects:</p>
  
  <pre><code>public class MasterObject {
    Long masterId;
    Long detailId;
    String attribute1;
    //...
  }
  
  public class DetailObject {
    Long detailId;
    String attribute2;
    //..
  }
  </code></pre>
  
  <p>I have a superset list of 10 master objects, and a subset list of 2 detail objects. In the end, I need a new set of 10 detail objects (copying information from the master), with the appropriate detail Id, after I've verified that attribute1 == attribute2. For each detail object, I need to create a new master object. This is what I've come up with so far, but it's incorrect. Please note my comment block:</p>
  
  <pre><code>List&lt;DetailObject&gt; updatedDetails = masterObjects.stream()
    .filter(master -&gt; {
      return detail.stream()
        .anyMatch(it -&gt; master.getAttribute1() == detail.getAttribute2());
    })
    .map(master -&gt; {
      DetailObject detailObject = new DetailObject();
      // Here is where I thought I could set the master Id in the detail object,
      // but I no longer have access to the original detail stream
       return detailObject;
     })
    .collect(Collectors.toList());
  </code></pre>
  
  <p>I'm pretty sure I need to do something after the filter, but filter of course returns just a boolean, when I need the object itself. What to do??</p>