title: Using the same list with streams twice in Java
body: <p>I have to complete this trivial operation with streams: given a list, get the sum and the sum of the first 20 elements.</p>
  
  <p>This is what I had in mind</p>
  
  <pre><code>IntStream stream = obj.stream().mapToInt(d-&gt;d.getInt());
  stream.limit(20).sum() / stream.sum();
  </code></pre>
  
  <p>However I cant do this, since I am told I can't reuse a stream, so.. I tried the following:</p>
  
  <pre><code>List&lt;Integer&gt; counts = obj.stream()
      .mapToInt(d -&gt; d.getInt())
      .boxed().collect(Collectors.toList());
  
  counts.stream().limit(20).sum() / counts.stream().sum();
  </code></pre>
  
  <p>However I am told that I can't use sum on Stream, so I need to mapToInt again for left and right hand side of this trivial operation.</p>
  
  <p>Is there a way to do this operation in a more elegant and concise way using streams?</p>