Question: Modify the program of Fig. 17.22 to summarize the number of occurrences of every character in the file. Fig. 17.22 1 // Fig. 17.22: StreamOfLines.java

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

Fig. 17.22

1 // Fig. 17.22: StreamOfLines.java 2 // Counting word occurrences in a text file. 3 import

1 // Fig. 17.22: StreamOfLines.java 2 // Counting word occurrences in a text file. 4 3 import java.io.IOException; import java.nio.file.Files; 5 import java.nio.file. Paths; 6 import java.util.Map; 7 import java.util.TreeMap; 8 import 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 wwwww N N N N 29 30 31 32 33 java.util.regex.Pattern; import java.util.stream.Collectors; public class StreamOfLines { public static void main(String[] args) throws IOException { // Regex that matches one or more consecutive whitespace characters Pattern pattern = Pattern.compile("\\s+"); 34 35 } } // 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.47 Rating (170 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The current program in the image is designed to count the occurrences of each word in a text file an... 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!