[英]System.out.println doesn't work on a supplied Stream?
我还是 Java 的新手,尤其是供应商的新手,但我不明白为什么我无法从以下代码中获得任何 output:
final BufferedReader brLines = new BufferedReader(new InputStreamReader(csvFile));
final Supplier<Stream<LinkedList<String>>> procLines = () -> brLines.lines().map(elm -> processCSV(elm));
lineCount = Math.toIntExact(procLines.get().count());
System.out.println(lineCount); // This prints the correct amount of lines to the console.
final CountDownLatch latch = new CountDownLatch(lineCount);
Stream<LinkedList<String>> listStream = procLines.get();
listStream.forEach((x) -> {
System.out.println(x); // Why is there no console output here?
outputText(() -> x); // Why is there no console output here either?
...
});
以下是此块中提到的一些方法
public static LinkedList<String> processCSV(String line) {
LinkedList<String> elms = new LinkedList<String>();
char delimiter = ',';
char quote = '"';
String[] elmArray = splitCSVWithQuote(line, delimiter, quote).toArray(new String[0]);
for (String elm : elmArray) {
elms.add(elm);
}
return elms;
}
&
public static void outputText(Supplier sup) {
System.out.println(sup.get());
}
任何人都可以提供任何帮助吗?
lineCount = Math.toIntExact(procLines.get().count());
count()
是一个终端操作,它可能会遍历 stream 产生结果。 终端操作完成后,stream流水线被视为消耗,不能再使用。
所以你消耗了文件的所有行。 所以,供应商不能再给你 stream 因为BufferedReader
现在处于流的末尾。 这就是为什么没有 output 的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.