title: Using Java 8 feature takes more time
body: <p>Sample Program provided to count number of elements from an array that are less than the specified value.
  Processing time taken by the program varies, using Java 8 forEach and stream takes more time for execution. Please explain if I should go with Java 8 features and if not which areas should it be avoided, additionally will parallelStream or multithreading with multi core processor help?</p>
  
  <p>Code:</p>
  
  <pre><code>public static void main(String[] args) {
      int lessThan = 4;
  
      // Using for loop iteration
      List&lt;Integer&gt; integerForSort1 = Arrays.asList(4, 1, 1, 2, 3);
      long startTime1 = System.nanoTime();
      long count1 = countNumbers(integerForSort1, lessThan);
      long stopTime1 = System.nanoTime();
      System.out.println(stopTime1 - startTime1);
      System.out.println(count1);
      integerForSort1 = null;
      System.gc();
  
      // Using binary search
      List&lt;Integer&gt; integerForSort2 = Arrays.asList(4, 1, 1, 2, 3);
      long startTime2 = System.nanoTime();
      long count2 = countByBinarySearch(integerForSort2, lessThan);
      long stopTime2 = System.nanoTime();
      System.out.println(stopTime2 - startTime2);
      System.out.println(count2);
      integerForSort2 = null;
      System.gc();
  
      // Using Java 8
      List&lt;Integer&gt; integerForSort3 = Arrays.asList(4, 1, 1, 2, 3);
      long startTime3 = System.nanoTime();
      long count3 = integerForSort3.stream()
              .filter(p -&gt; p &lt; lessThan)
              .count();
      long stopTime3 = System.nanoTime();
      System.out.println(stopTime3 - startTime3);
      System.out.println(count3);
      integerForSort3 = null;
      System.gc();
  
      //Using Java 8 for each loop
      List&lt;Integer&gt; integerForSort4 = Arrays.asList(4, 1, 1, 2, 3);
      long startTime4 = System.nanoTime();
      long count4 = process(integerForSort4, p -&gt; p &lt; lessThan);
      long stopTime4 = System.nanoTime();
      System.out.println(stopTime4 - startTime4);
      System.out.println(count4);
      integerForSort4 = null; 
  }
  
  public static long countNumbers(List&lt;Integer&gt; integerForSort, int lessThan) {
      long count = 0;
      Collections.sort(integerForSort);
      for (Integer anIntegerForSort : integerForSort) {
          if (anIntegerForSort &lt; lessThan)
              count++;
      }
      return count;
  }
  
  public static long countByBinarySearch(List&lt;Integer&gt; integerForSort, int lessThan){
      if(integerForSort==null||integerForSort.isEmpty())
          return 0;
      int low = 0, mid = 0, high = integerForSort.size();
      Collections.sort(integerForSort);
      while(low != high){
          mid = (low + high) / 2;
  
          if (integerForSort.get(mid) &lt; lessThan) {
              low = mid + 1;
          }
          else {
              high = mid;
          }
      }
      return low;
  }    
  
  public static long process(List&lt;Integer&gt; integerForSort, Predicate&lt;Integer&gt; predicate) {
      final AtomicInteger i = new AtomicInteger(0);
      integerForSort.forEach((Integer p) -&gt; {
          if (predicate.test(p)) {
              i.getAndAdd(1);
          }
      });
  
      return i.intValue();
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>345918
  4
  21509
  4
  29651234
  4
  2242999
  4
  </code></pre>
  
  <p>Questions:</p>
  
  <pre><code>Is it possible to reduce the process time using Java 8 features?
  Why does Java 8 stream takes more time?
  How can I use lambda expression with binary Search, will it process faster?
  </code></pre>
  
  <p>Even using multithreading with concurrent.ExecutorService gave consistent results:</p>
  
  <pre><code>Result 4 : Thread 'pool-1-thread-1' ran process - Using Java 8 stream in 6 millisecond, from 11:16:05:361 to 11:16:05:367
  Result 4 : Thread 'pool-1-thread-2' ran process - Using Java 8 forEach in 3 millisecond, from 11:16:05:361 to 11:16:05:364
  Result 4 : Thread 'pool-1-thread-4' ran process - Using Java 7 binary Search in 0 millisecond, from 11:16:05:379 to 11:16:05:379
  Result 4 : Thread 'pool-1-thread-3' ran process - Using Java 7 for loop in 1 millisecond, from 11:16:05:362 to 11:16:05:363
  </code></pre>