Solution based on Files.readAllLines()

If memory (RAM) is not a problem for us, then we can try to read the entire file into memory (via Files.readAllLines()) and process it from there. Having the entire file in memory sustains parallel processing. Therefore, if our hardware can be highlighted by parallel processing, then we can try to rely on parallelStream(), as follows:

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

return Files.readAllLines(path, ch).parallelStream()
.mapToInt((p) -> countStringInString(p, text))
.sum();
}
If parallelStream() doesn't come with any benefits, then we can simply switch to stream(). It is just a matter of benchmarking.
..................Content has been hidden....................

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