I am new to the streams and I have the requirement to count a particular String in each line of a text file using Java 8 Stream. Does anyone have any suggestions or sample code? Edit: I missed to add my sample code..Here is the code I have just started but I have no clue as how to use count() on the stream.
Stream<String> lines=Files.lines(Paths.get("M:\\GIT_CODE\\FIRE_CHAT_PARSE\\New Text Document.txt"));
lines.map(eachLine-> spliteLineToArrayofSTrFunc(eachLine)).forEach(eachLines->{
System.out.println(eachLines);
System.out.println();
});
private static Function<String, String[]> spliteLineToArrayofSTrFunc(String line) {
return (String lines)->{
System.out.println();
return line.split(" ");
};
}
countunder theStreamclass. But remember, you might want tofilterspecific string before you count.lapse? Do you want the count to include the wordcollapse? What if the line includes the string more than once? You need to be specific in what you want. Perhaps show some examples.