System.out.println在提供的流上不起作用?

huangapple go评论74阅读模式
英文:

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&lt;Stream&lt;LinkedList&lt;String&gt;&gt;&gt; procLines = () -&gt; brLines.lines().map(elm -&gt; 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&lt;LinkedList&lt;String&gt;&gt; listStream = procLines.get();
listStream.forEach((x) -&gt; {
    System.out.println(x); // Why is there no console output here?
	outputText(() -&gt; x); // Why is there no console output here either?

    ...
});

Here are some of the methods mentioned in this block

public static LinkedList&lt;String&gt; processCSV(String line) {	
	LinkedList&lt;String&gt; elms = new LinkedList&lt;String&gt;();
	char delimiter = &#39;,&#39;;
	char quote = &#39;&quot;&#39;;

	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.

huangapple
  • 本文由 发表于 2020年8月7日 00:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63287527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定