Count all the words in a file using java Streams
You are mapping each line to an array (transforming a Stream<String>
to a Stream<String[]>
, and then count the number of array elements (i.e. the number of lines in the file).
You should use flatMap
to create a Stream<String>
of all the words in the file, and after the distinct()
and count()
operations, you'll get the number of distinct words.
long wordCount =
Files.lines(Paths.get("sample.txt"))
.flatMap(line -> Arrays.stream(line.split("\\s+")))
.distinct()
.count();
You seem to be counting the lines in your file instead :
map(line -> line.split("\\s+")) // this is a Stream<String[]>
You shall further use Stream.flatMap
as:
long wordCount = Files.lines(Paths.get("sample.txt"))
.map(line -> line.split("\\s+"))
.flatMap(Arrays::stream)
.distinct()
.count();