title: When should I use streams?
body: <p>I just came across a question when using a <code>List</code> and its <code>stream()</code> method. While I know <em>how</em> to use them, I'm not quite sure about <em>when</em> to use them.</p>
  <p>For example, I have a list, containing various paths to different locations. Now, I'd like to check whether a single, given path contains any of the paths specified in the list. I'd like to return a <code>boolean</code> based on whether or not the condition was met.</p>
  <p>This of course, is not a hard task per se. But I wonder whether I should use streams, or a for(-each) loop.</p>
  <p><strong>The List</strong></p>
  <pre class="lang-java prettyprint-override"><code>private static final List&lt;String&gt; EXCLUDE_PATHS = Arrays.asList(
      &quot;my/path/one&quot;,
      &quot;my/path/two&quot;
  );
  </code></pre>
  <p><strong>Example using Stream:</strong></p>
  <pre class="lang-java prettyprint-override"><code>private boolean isExcluded(String path) {
      return EXCLUDE_PATHS.stream()
                          .map(String::toLowerCase)
                          .filter(path::contains)
                          .collect(Collectors.toList())
                          .size() &gt; 0;
  }
  </code></pre>
  <p><strong>Example using for-each loop:</strong></p>
  <pre class="lang-java prettyprint-override"><code>private boolean isExcluded(String path){
      for (String excludePath : EXCLUDE_PATHS) {
          if (path.contains(excludePath.toLowerCase())) {
              return true;
          }
      }
      return false;
  }
  </code></pre>
  <p>Note that the <code>path</code> parameter is always <em>lowercase</em>.</p>
  <p>My first guess is that the for-each approach is faster, because the loop would return immediately, if the condition is met. Whereas the stream would still loop over all list entries in order to complete filtering.</p>
  <p>Is my assumption correct? If so, <em>why</em> (or rather <em>when</em>) would I use <code>stream()</code> then?</p>