title: How to validate that a Java 8 Stream has two specific elements in it?
body: <p>Let's say I have <code>List&lt;Car&gt;</code> and I want to search through that list to verify that I have both a Civic AND a Focus. If it's an OR it's very easy in that I can just apply an OR on the <code>.filter()</code>. Keep in mind that I can't do <code>filter().filter()</code> for this type of AND. </p>
  
  <p>A working solution would be to do:</p>
  
  <pre><code>boolean hasCivic = reportElements.stream()
          .filter(car -&gt; "Civic".equals(car.getModel()))
          .findFirst()
          .isPresent();
  
  boolean hasFocus = reportElements.stream()
          .filter(car -&gt; "Focus".equals(car.getModel()))
          .findFirst()
          .isPresent();
  
  return hasCivic &amp;&amp; hasFocus;
  </code></pre>
  
  <p>But then I'm basically processing the list twice. I can't apply an <code>&amp;&amp;</code> in the filter nor can I do <code>filter().filter()</code>. </p>
  
  <p>Is there a way to process the stream once to find if the list contains both a Civic and a Focus car?</p>
  
  <p><strong>IMPORTANT UPDATE:</strong> The key problem with the solutions provided is that they all guarantee O(n) whereas my solution could be done after just two comparisons. If my list of cars is say 10 million cars then there would be a very significant performance cost. My solution however doesn't feel right, but maybe it is the best solution performance wise...</p>