Solution based on Files.lines()

We can try to take advantage of streams via Files.lines() as well. This time, we fetch the file as a lazy Stream<String>. If we can take advantage of parallel processing (benchmarking reveals better performances), then it is very simple to parallelize Stream<String> by calling the parallel() method:

public static int countOccurrences(Path path, String text, Charset ch)
throws IOException {

return Files.lines(path, ch).parallel()
.mapToInt((p) -> countStringInString(p, text))
.sum();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138