我想使用Java 8流獲取字符串流(例如從純文本文件讀取)並生成一串句子。我假設句子可以跨越界限。Java 8句子流
因此,舉例來說,我想去來自:
"This is the", "first sentence. This is the", "second sentence."
到:
"This is the first sentence.", "This is the second sentence."
我可以看到,它可能得到句子的一部分流如下:
Pattern p = Pattern.compile("\\.");
Stream<String> lines
= Stream.of("This is the", "first sentence. This is the", "second sentence.");
Stream<String> result = lines.flatMap(s -> p.splitAsStream(s));
但後來我不知道如何產生一個流將片段加入句子。我想以懶惰的方式做到這一點,以便只讀取原始流所需的內容。有任何想法嗎?
是否有必要使用正則表達式?你有沒有嘗試使用StringBuilder和String.split? – 10101010