title: Using Java 8 Supplier in streams to achieve lazy evaluation
body: <p>I am trying to achieve lazy evaluation using Supplier in a stream like this</p>
  
  <pre><code>public static void main(String[] args) {
  
          Supplier&lt;List&lt;String&gt;&gt; expensiveListSupplier= getExpensiveList();
          getList().stream()
                  .filter(s -&gt; expensiveListSupplier.get().contains(s))
                  .forEach(System.out::println);
      }
  
      private static Supplier&lt;List&lt;String&gt;&gt; getExpensiveList() {
          return () -&gt; Stream
                  .of("1", "2", "3")
                  .peek(System.out::println)
                  .collect(Collectors.toList());
      }
  
      private static List&lt;String&gt; getList() {
          return Stream.of("2", "3")
                  .collect(Collectors.toList());
      }
  </code></pre>
  
  <p>But this will call getExpensiveList() method for every element in the list.
  I am trying not to be verbose and don't want to write something like this ie to add not empty checks and stuff.</p>
  
  <pre><code>public static void main(String[] args) {
  
          Supplier&lt;List&lt;String&gt;&gt; expensiveListSupplier = getExpensiveList();
          List&lt;String&gt; list = getList();
          if (!list.isEmpty()) {
              List&lt;String&gt; expensiveList = expensiveListSupplier.get();
              list.stream()
                      .filter(expensiveList::contains)
                      .forEach(System.out::println);
          }    
      }
  
      private static Supplier&lt;List&lt;String&gt;&gt; getExpensiveList() {
          return () -&gt; Stream
                  .of("1", "2", "3")
                  .peek(System.out::println)
                  .collect(Collectors.toList());
      }
  
      private static List&lt;String&gt; getList() {
          return Stream.of("2", "3")
                  .collect(Collectors.toList());
      }
  </code></pre>