title: How to compute the hash code for a stream in the same way as List.hashCode()
body: <p>I just realized that implementing the following algorithm to compute the hash code for a stream is not possible using <a href="https://docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html#reduce-T-java.util.function.BinaryOperator-" rel="noreferrer">Stream.reduce(...)</a>. The problem is that the initial seed for the hash code is <code>1</code> which is not an identity for the accumulator.</p>
  
  <p>The algorithm for <a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html#hashCode--" rel="noreferrer">List.hashCode()</a>
  :</p>
  
  <pre><code>int hashCode = 1;
  for (E e : list)
    hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
  </code></pre>
  
  <p>You might be tempted to think that the following is correct but it isn't, although it will work if the stream processing is not split up.</p>
  
  <pre><code>List&lt;Object&gt; list = Arrays.asList(1,null, new Object(),4,5,6);
  int hashCode = list.stream().map(Objects::hashCode).reduce(1, (a, b) -&gt; 31 * a + b);
  </code></pre>
  
  <p>It seems that the only sensible way of doing it would be to get the <code>Iterator</code> of the <code>Stream</code> and do normal sequential processing or collect it to a <code>List</code> first.</p>