英文:
System.out.println doesn't work on a supplied Stream?
问题
我仍然对Java很陌生,尤其是对Suppliers很陌生,但我弄不明白为什么无法从以下代码中获得任何输出:
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());
}
有人能提供帮助吗?
英文:
I'm still new to Java, and especially new to Suppliers, but I can't figure out why I can't get any output from the following code:
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?
...
});
Here are some of the methods mentioned in this block
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());
}
Can anyone provide any help?
答案1
得分: 2
lineCount = Math.toIntExact(procLines.get().count());
count()
是一个终端操作,它可能会遍历流来生成结果。在执行终端操作之后,流管道被视为已消耗,不再可以使用。
因此,您已经消耗了文件的所有行。所以,供应商无法再次为您提供流,因为 BufferedReader
现在已经到达流的末尾。这就是为什么没有输出的原因。
英文:
lineCount = Math.toIntExact(procLines.get().count());
count()
is a terminal operation, it may traverse the stream to produce a result. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used.
So you consumed all the lines of the file. So, the supplier can't give you again the stream since BufferedReader
is now at the end-of-stream. That's why there is no output.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论