title: Iterate with streams (with a flag in for) [keep state within lambda]
body: <p>I have two lists, allowedOU and parts. I need to know how to iterate through streams, check the condition and if it is true, include the element in a third list, and change the flag (heritable).</p>
  <pre class="lang-java prettyprint-override"><code>for (String part : parts) {
      for (BeanOU it : allowedOU) {
          if (part.startsWith(&quot;OU&quot;) &amp;&amp; it.OU.equals(part.substring(3) &amp;&amp; heritableFlag) {
              list.add(part.substring(3, part.length()));
              heritableFlag = it.heritable;
              break;
          }
      }
  }
  </code></pre>
  <p>I tried something like this</p>
  <pre><code>parts.stream()
          .filter(parte -&gt; allowedOU.stream()
                  .anyMatch(allowed -&gt; (parte.startsWith(&quot;OU&quot;)) 
                          &amp;&amp; allowed.OU.equals(parte.substring(3, parte.length())) 
                   &amp;&amp; finalHeritableFlag))
          .forEach(here we don't have it variable...)
  </code></pre>