Java 8 added a new method splitAsStream() to the java.util.regex.Pattern class, which takes a sequence of characters and splits it into a stream, according to the formula we hand it. There’s a constraint, which is that the input is a CharSequence , so we cannot feed a stream into splitAsStream().
// streams/FileToWordsRegexp.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.io.*;
import java.nio.file.*;
import java.util.regex.Pattern;
import java.util.stream.*;
public class FileToWordsRegexp {
private String all;
public FileToWordsRegexp(String filePath) throws Exception {
all =
Files.lines(Paths.get(filePath))
.skip(1) // First (comment) line
.collect(Collectors.joining(" "));
}
public Stream<String> stream() {
return Pattern.compile("[ .,?]+").splitAsStream(all);
}
public static void main(String[] args) throws Exception {
FileToWordsRegexp fw = new FileToWordsRegexp("Cheese.dat");
fw.stream().limit(7).map(w -> w + " ").forEach(System.out::print);
fw.stream().skip(7).limit(2).map(w -> w + " ").forEach(System.out::print); // map() method return the new stream
}
}
/* Output:
Not much of a cheese shop really is it
*/
The limit here is that the whole file must be stored in memory; most of the time that probably won’t be an issue but it loses important benefits of streams:
- They “don’t require storage.” Of course they actually require some internal storage, but it’s only a fraction of the sequence, and nothing like what it takes to hold the entire sequence.
- They are lazily evaluated.
the solution is here - FileToWord by using flatMap() method.
see previoue example.
references:
1. On Java 8 - Bruce Eckel
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Cheese.dat
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/FileToWordsRegexp.java
5. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#limit-long-
7. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#skip-long-
本文介绍Java8中java.util.regex.Pattern类新增的splitAsStream()方法,该方法允许开发者将字符序列根据指定公式拆分为流。通过实例演示如何从文件读取文本,使用正则表达式分割并转换为流,最后展示如何限制和跳过流中的元素。
9586

被折叠的 条评论
为什么被折叠?



