Question: Reimplement Fig. 17.22 using parallel streams. Use the Date/Time API timing techniques to compare the time required for the sequential and parallel versions of the

Reimplement Fig. 17.22 using parallel streams. Use the Date/Time API timing techniques to compare the time required for the sequential and parallel versions of the program.

Fig. 17.22

12345 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Paths; 6 import

12345 2 3 4 13 14 15 5 import java.nio.file.Paths; 6 import java.util.Map; 7 import java.util.TreeMap; 8 import java.util. regex .Pattern; java.util.stream.Collectors; 9 import 10 II public class StreamOfLines { 12 16 17 18 19 20 NNNNN200 21 22 23 24 25 // Fig. 17.22: StreamOfLines.java // Counting word occurrences in a text file. import java.io.IOException; import java.nio.file.Files; 26 27 28 29 30 31 32 33 34 35 } public static void main(String[] args) throws IOException { // Regex that matches one or more consecutive whitespace characters Pattern pattern = Pattern.compile("\\s+"); } // count occurrences of each word in a Stream sorted by word Map wordCounts = Files.lines (Paths.get("Chapter 2Paragraph. txt")) .flat Map (line -> pattern.splitAsStream (line)) .collect (Collectors.grouping By (String::toLowerCase, TreeMap::new, Collectors.counting (())); // display the words grouped by starting letter wordCounts.entrySet() .stream() .collect( Collectors.grouping By (entry entry.getKey(.charAt(0), TreeMap:: new, Collectors.toList())) .forEach((letter, wordList) -> { System.out.printf("%n%C%n", letter); wordList.stream().forEach (word -> System.out.printf( "%13s: %d%n", word.getKey(), word.getValue(())); });

Step by Step Solution

3.40 Rating (144 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The provided image depicts Java code for a program StreamOfLines that reads a text file and counts the occurrences of each word in a sequential manner using stream operations To reimplement the code u... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!