Summarizing the Occurrences of Each Word in the File

Lines 19–24 summarize contents of the text file "Chapter2Paragraph.txt" (which is located in the folder with the example) into a Map<String, Long> in which each String key is a word in the file and the corresponding Long value is the number of occurrences of that word. The statement performs the following tasks:

• Line 20 uses Files method lines to create a Stream<String> for reading the lines of text from a file. Class Files (package java.nio.file) is one of many classes throughout the Java APIs that have been enhanced to support Streams.

• Line 21 uses Stream method map to remove all the punctuation, except apostrophes, in the lines of text. The lambda argument represents a Function that ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.