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-