title: Java Lambda Expression for Nested Loops with Conditional
body: <p>I am new to lambda expressions and am trying to use them to reduce the following code to the lambda equivalent.  I have looked into reduce and flatMap and forEach, as well as several other things, but I am obviously missing something because everything that I try is either syntactically incorrect or I don't have a reference for what I need.</p>
  
  <p>I need to perform an analysis of each element against all other elements in a collection.  I coded that as nested loops with a conditional.  Once non-matching elements have been identified, a computation is done using both elements.  Finally, I want a collection of results for each comparative computation.</p>
  
  <p>So, here's the original code:</p>
  
  <pre><code>final List&lt;Element&gt; updated = new ArrayList&lt;&gt;(elements.size());
  
  for (final Element first : elements) {
      Attribute newAttribute = first.getAttribute();
  
      for (final Element second : elements) {
          if (!first.equals(second)) {
              newAttribute = newAttribute.add(computeChange(first, second));
          }
      }
      final Element newElement = new Element(first.getEntry(), newAttribute, first.getValue());
      updated.add(newElement);
  }
  </code></pre>
  
  <p>Then, I tried many variations of lambda expressions, the simplest of which is:</p>
  
  <pre><code>elements.parallelStream()
           .map(first -&gt; new Element(first.getEntry(), first.getAttribute().add(
           computeChange(first, second)), first
           .getValue())).collect(Collectors.toList()));
  </code></pre>
  
  <p>Obviously, this is wrong as there is no reference to second available to me and no condition/filter for second being not equal to first.</p>
  
  <p>How do I reduce this nested loop with conditional returning a collection to a lambda expression?</p>
  
  <p>Any help here is greatly appreciated.</p>