Count all the words in a file using java Streams

  • A+

I was trying to count the number of unique words in a text file. For the sake of simplicity, my current file content is:

This is a sample file

My attempt is:

long wordCount =      Files.lines(Paths.get("sample.txt"))          .map(line -> line.split("//s+"))          .distinct()          .count(); System.out.println(wordCount); 

This compiles and runs fine, but results in 1, while it should be 5.


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 ->"//s+")))          .distinct()          .count(); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: