我正在尝试执行流的嵌套循环。当我在列表中收集结果时,它抛出“非法状态异常”,声明steam已关闭
public void execute(Stream<Trade> trade, Stream<Order> order){
order.filter(o -> trade.anyMatch(t -> t.getInstrumentId() ==
o.getInstrumentId() && t.getGroupid() == o.getGroupId()))
.collect(Collectors.toList());// Illegal exception only when i include this line
}异常为: java.lang.IllegalStateException:流已被操作或关闭
我知道我们不应该多次使用流,但在这种情况下,我只是过滤,然后收集..在过滤之前,它不会显示错误,但在收集它时会抛出此异常。
发布于 2018-07-13 14:51:50
order stream is used trade stream is not对于order流的每个元素,都需要根据trade的内容检查条件,但只能迭代一次,这就是问题所在。
您可以传递来自trade流的Collection。
https://stackoverflow.com/questions/51316941
复制相似问题