Question: Java: Modify method showIndex so each output line displays a word followed by a comma and a list of line numbers separated by commas. You
Java: Modify method showIndex so each output line displays a word followed by a comma and a list of line numbers separated by commas. You can either edit the string corresponding to each Map entry before displaying it or use methods Map.Entry.getKey and Map.Entry.getValue to build a different string.
EXAMPLE 7. 11 In Example 7.4 we showed how to use a Map to build an index for a term paper. Because we want to display the words of the index in alphabetical order, we must store the index in a Sorted Map. Method showIndex below displays the string representation of each index entry in the form key value If the word fire appears on lines 4, 8, and 20, the corresponding output line would be fire [4, 8, 20] It would be relatively easy to display this in the more common form: fire 4, 8, 20 (see Programming Exercise 4) Displays the index, one word per line public void showIndexC) index. forEach (Ck, v) System. out.println(k v)) The Map. forEach method applies a Biconsumer (See Table 6.2) to each map key-value pair. In the lambda expression (k, v) System. out. println(k vD the parameter k is bound to the key, and the parameter v i bound to the value. Without Java 8, the enhanced for loop could be used but the code would be more cumbersome: Displays the index one word per line th/ public void showIndex O for GMap. Entry
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
