为什么我会有下一个例外?
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)...com.search.offer.OffersSelector.isGood(OffersSelector.java:23)如何修改代码来修复它?
Stream<String> titleExclusions = ResourceUtility.contentToUtf8TreeSet("+.txt").
stream().filter(item -> item.length() == 0).collect(Collectors.toSet()).stream();
//...
titleExclusions.filter(tittle::contains).collect(Collectors.toSet()).size() == 0;//line 23发布于 2016-04-12 14:23:14
您不能在流上操作不止一次,所以最好使用集合,因为它们可以不止一次使用。
Set<String> titleExclusions = ResourceUtility.contentToUtf8TreeSet("+.txt")
.stream()
.filter(item -> !item.isEmpty())
.collect(Collectors.toSet());
// uses titleExclusions
boolean noMatches = titleExclusions.stream()
.noneMatch(tittle::contains);
// uses titleExclusions again.注意:我假设您想要源文件中的非空行,而不是一组空白行。filter获取保留的内容的Predicate,而不是丢弃的内容。
谢谢你@Holger简化了第二个陈述。
https://stackoverflow.com/questions/36575679
复制相似问题