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
.