... corresponding Long value is the number of occurrences of that word. The pipeline performs the following tasks:

  • Line 18 calls Files method lines (added in Java SE 8) which returns a Stream<String> that reads lines of text from a file and returns each line as a String. Class Files (package java.nio.file) is one of many classes throughout the Java APIs which provide methods that return Streams.

  • Line 19 uses Stream method flatMap to break each line of text into its separate words. Method flatMap receives a Function that maps an object into a stream of elements. In this case, the object is a String containing words and the result is a Stream<String> for the individual words. The lambda in line 19 passes the String representing a line of text to ...

Get Java How to Program, Early Objects, 11th 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.