title: java 8 stream execution vs traditional loop performance
body: <p>I have requirement that to read data from properties file which is command separated which will be obviously in string form. So after that I am splitting it into string array.</p>
  <p>I have requirement to convert that string array to integer list.</p>
  <p>I have tried 2 ways:</p>
  <ol>
  <li>Traditional for each loop with casting,</li>
  <li>use java 8 stream.</li>
  </ol>
  <p>Then I think I have to calculate the time which one is good as per performance. So I have some rough data.</p>
  <p>Code:</p>
  <pre><code>public class PropertyLoadCSV {
      private static Properties properties;
      public static void main(String[] args) {
          try {
              properties = new Properties();
              properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(&quot;resources/csvfile.properties&quot;));
  
              String ids = properties.getProperty(&quot;ids&quot;);
              String[] splitedIDs = ids.split(&quot;,&quot;);
  
              long startTime = System.nanoTime();
              convertUsingJava7(splitedIDs);
              long endTime = System.nanoTime();
              long duration = (endTime - startTime);
              System.out.println(duration);
  
              long startTime1 = System.nanoTime();
              convertUsingJava8(splitedIDs);
              long endTime1 = System.nanoTime();
              long duration1 = (endTime1 - startTime1);
              System.out.println(duration1);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      static List&lt;Integer&gt; convertUsingJava7(String[] splited){
          List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
          for(String s: splited){
              list.add(Integer.valueOf(s));
          }
          return list;
      }
      static List&lt;Integer&gt; convertUsingJava8(String[] splited){
          return Stream.of(splited).map(Integer::parseInt).collect(Collectors.toList());
      }
  }
  </code></pre>
  <p>got some unexpected result in console:</p>
  <blockquote>
  <p>131601</p>
  <p>254094088</p>
  </blockquote>
  <p>so really java 8 stream convert and cast slowly then tradition way ?</p>
  <p>Which one I have to use as <strong>performance</strong> concern ?</p>
  <p><a href="http://www.tutorialspoint.com/compile_java8_online.php?PID=0Bw_CjBb95KQMaHZtN1Bfc29mb0k" rel="nofollow noreferrer">Here's</a> working example for it.</p>