如何在 Java 8 中比较两个流
2022-09-01 00:10:19
比较Java 8中的两个实例并找出它们是否具有相同的元素(专门用于单元测试)的好方法是什么?Stream
我现在拥有的是:
@Test
void testSomething() {
Stream<Integer> expected;
Stream<Integer> thingUnderTest;
// (...)
Assert.assertArrayEquals(expected.toArray(), thingUnderTest.toArray());
}
或者:
Assert.assertEquals(
expected.collect(Collectors.toList()),
thingUnderTest.collect(Collectors.toList()));
但这意味着我正在构建两个集合并丢弃它们。考虑到我的测试流的大小,这不是性能问题,但我想知道是否有一种规范的方法来比较两个流。