How to compare two Streams in Java 8
<p>What would be a good way to compare two <code>Stream</code> instances in Java 8 and find out whether they have the same elements, specifically for purposes of unit testing?</p>

<p>What I've got now is:</p>

<pre><code>@Test
void testSomething() {
  Stream&lt;Integer&gt; expected;
  Stream&lt;Integer&gt; thingUnderTest;
  // (...)
  Assert.assertArrayEquals(expected.toArray(), thingUnderTest.toArray());
}
</code></pre>

<p>or alternatively:</p>

<pre><code>Assert.assertEquals(
    expected.collect(Collectors.toList()),
    thingUnderTest.collect(Collectors.toList()));
</code></pre>

<p>But that means I'm constructing two collections and discarding them. It's not a performance issue, given the size of my test streams, but I'm wondering whether there's a canonical way to compare two streams.</p>