如何比较 Java 8 中的两个流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34818533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two Streams in Java 8
提问by Vlad
What would be a good way to compare two Streaminstances in Java 8 and find out whether they have the same elements, specifically for purposes of unit testing?
什么是比较StreamJava 8 中的两个实例并找出它们是否具有相同元素的好方法,特别是出于单元测试的目的?
What I've got now is:
我现在有的是:
@Test
void testSomething() {
Stream<Integer> expected;
Stream<Integer> thingUnderTest;
// (...)
Assert.assertArrayEquals(expected.toArray(), thingUnderTest.toArray());
}
or alternatively:
或者:
Assert.assertEquals(
expected.collect(Collectors.toList()),
thingUnderTest.collect(Collectors.toList()));
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.
但这意味着我正在构建两个集合并丢弃它们。考虑到我的测试流的大小,这不是性能问题,但我想知道是否有比较两个流的规范方法。
采纳答案by ZhongYu
static void assertStreamEquals(Stream<?> s1, Stream<?> s2)
{
Iterator<?> iter1 = s1.iterator(), iter2 = s2.iterator();
while(iter1.hasNext() && iter2.hasNext())
assertEquals(iter1.next(), iter2.next());
assert !iter1.hasNext() && !iter2.hasNext();
}
回答by erickson
Collecting the stream under test (as you show) is a straightforward and effective way of performing the test. You may create the list of expected results in the easiest way available, which might not be collecting a stream.
收集被测流(如您所示)是执行测试的一种直接而有效的方法。您可以以可用的最简单方式创建预期结果列表,这可能不是收集流。
Alternatively, with most libraries for creating mock collaborators, one could mock a Consumerthat "expects" a series of accept()calls with particular elements. Consumethe Streamwith it, and then "verify" that its configured expectations were met.
或者,对于大多数用于创建模拟协作者的库,可以模拟Consumer“期望”一系列accept()具有特定元素的调用。消耗了Stream它,然后“确认”其配置的期望得到满足。
回答by Frank Neblung
You can assert the stream's content withoutcreating a Stream<> expected.
您可以断言流的内容而无需创建Stream<> expected.
assertjhas fluent and readable solutions for this.
assertj 对此有流畅和可读的解决方案。
import static org.assertj.core.api.Assertions.assertThat;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
class MyTests {
@Test
void test() {
Stream<Integer> actual = Stream.of(0, 8, 15); // your thingUnderTest
assertThat(actual).containsExactly(0, 8, 15);
}
}
回答by Marc Dzaebel
public static boolean equalStreams(Stream<?>...streams) {
List<Iterator<?>>is = Arrays.stream(streams).map(Stream::iterator).collect(Collectors.toList());
while(is.stream().allMatch(Iterator::hasNext))
if(is.stream().map(Iterator::next).distinct().limit(2).count()>1) return false;
return is.stream().noneMatch(Iterator::hasNext);
}
回答by Lii
Using the elementsEqualmethod in the Guava library:
使用elementsEqualGuava库中的方法:
Iterators.elementsEqual(s1.iterator(), s2.iterator())
回答by Arpan Saini
How to Compare Two Streams in java 8 and above: with the example of Comparing IntStream
java 8及以上版本如何比较两个流:以比较IntStream为例
package com.techsqually.java.language.generics.basics;
import java.util.Iterator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class TwoStreamComparision {
public static void main(String[] args) {
String a = "Arpan";
String b = "Arpen";
IntStream s1 = a.chars();
IntStream s2 = b.chars();
Iterator<Integer> s1It = s1.iterator();
Iterator<Integer> s2It = s2.iterator();
//Code to check how many characters are not equal in both the string at their Respective Position
int count = 0;
while (s2It.hasNext()){
if (!s1It.next().equals(s2It.next())){
count++;
}
}
System.out.println(count);
}
}

