[英]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.