title: Optionally call .distinct() in the middle of a Streams API function call chain
body: <p>I cannot find a way to use the Streams API <strong>and</strong> to have a short conditional addition to the 'pipeline' of an aggregate operation like a .distinct() call.</p>
  <p>The following code shows how I have to duplicate the .filter call after the conditional .distinct() call, making the code more verbose than I'd like:</p>
  <pre class="lang-java prettyprint-override"><code>AtomicInteger index = new AtomicInteger();
  Supplier&lt;Stream&lt;NodeXType&gt;&gt; nodexSupplier = () -&gt; myXMLPayload.getNodeXs().stream();
  // Avoid BUG where the NodeXs are duplicated in the Payload XML impacting tests.
  if (NODE_X.length &lt; nodexSupplier.get().filter(e -&gt; e.getSubTypeofNode_X().equalsIgnoreCase(&quot;myTargetValue&quot;)).count()){
     LOGGER.log(Level.WARNING, &quot;had more sub-type values than expected, so removed duplicates&quot;);
     nodexSupplier.get()
         .distinct()
         .filter(e -&gt; e.getSubTypeofNode_X().equalsIgnoreCase(&quot;myTargetValue&quot;))
         .forEachOrdered(e -&gt; {
             assertEquals(&quot; myTargetValue nodes not updated to expected value&quot;,
                 e.getSubTypeofNode_X().getNewValue().toString(), NODE_X[index.getAndIncrement()]);
         });
  } else {
     nodexSupplier.get()
         .filter(e -&gt; e.getSubTypeofNode_X().equalsIgnoreCase(&quot;myTargetValue&quot;))
         .forEachOrdered(e -&gt; {
             assertEquals(&quot; myTargetValue nodes not updated to expected value&quot;,
                 e.getSubTypeofNode_X().getNewValue().toString(), NODE_X[index.getAndIncrement()]);
         });
  }
  </code></pre>
  <p>Is there a way to avoid this, without breaking the Streams rules?
  like:</p>
  <pre><code>pipelineObj =  Source.stream()    
  if (condition) {pipelineObj.distinct}
  pipelineObj.filter{}...
  </code></pre>