Question: Reimplement Exercise 17.10 using parallelstreams. Use the Date/Time API timing techniques to compare the time required for the sequential and parallel versions of the program.

Reimplement Exercise 17.10 using parallelstreams. Use the Date/Time API timing techniques to compare the time required for the sequential and parallel versions of the program.

Exercise 17.10

Modify the program of Fig. 17.22 to summarize the number of occurrences of every character in the file.

Fig. 17.22

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

1234 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Paths; 6 import java.util.Map; 7 import java.util.TreeMap; import 8 9 13 14 15 10 II public class StreamOfLines { 12 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 // Fig. 17.22: StreamOfLines.java // Counting word occurrences in a text file. NNNNNNMMMM MM 33 34 java.util.regex.Pattern; import java.util.stream.Collectors; 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")) .flatMap (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.39 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To reimplement Exercise 1710 using parallel streams we need to modify the stream operations to their parallel counterparts and add timing mechanisms t... 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!