Read all words from a file and add them to a map whose keys are the first
Question:
Read all words from a file and add them to a map whose keys are the first letters of the words and whose values are sets of words that start with that same letter. Then print out the word sets in alphabetical order.
Provide two versions of your solution, one that uses the merge method (see Special Topic 15.1) and one that updates the map as in Worked Example 15.1.
Data from special topic 15.1
Data from worked example 15.1.
Transcribed Image Text:
Special Topic 15.1| Updating Map Entries Maps are commonly used for counting how often an item occurs. For example, Worked Example 15.1 uses a Map to track how many times a word occurs in a file. It is a bit tedious to deal with the special case of inserting the first value. Consider the fol- lowing code from Worked Example 15.1: Integer count = frequencies.get(word); // Get the old frequency count // If there was none, put 1; otherwise, increment the count if (count == null) { count = 1; } else { count count + 1; } frequencies.put (word, count); This task can be simplified with the merge method of the Map interface. You specify A key. A value to be used if the key is not yet present. A function to compute the updated value if the key is present. The function is specified as a lambda expression (see Special Topic 10.4). For example, frequencies.merge (word, 1, (oldValue, no tPresent Value) -> oldValue + not PresentValue); does the same as the four lines of code above. If word is not present, the value is set to 1. Other- wise, the old value is incremented. The merge method is also useful if the map values are sets or comma-separated strings-see Exercises E15.6 and .. E15.7.
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (3 reviews)
To address the question using the referenced materials Ill provide two versions of Java code that accomplish the given task Each version will read wor...View the full answer
Answered By
RADHIKA MEENAKAR
I am a qualified indian Company Secretary along with Masters in finance with over 6 plus years of professional experience. Apart from this i am a certified accounts and finance tutor on many online platforms.
My Linkedin profile link is here https://www.linkedin.com/in/radhika-meenakar-88b9808a/
5.00+
12+ Reviews
22+ Question Solved
Related Book For
Question Posted:
Students also viewed these Java Programming questions
-
Read all words from a file and add them to a map whose keys are word lengths and whose values are comma-separated strings of words of the same length. Then print out those strings, in increasing...
-
123 Compare the purely graphical properties of these two notations, and the ways in which the graphical properties of each display correspond to the information structure being defined. Describe...
-
for the equality test for M and N. The question concerns how to represent multisets of strings within ML. For each of the given data representations (a), (b) and (c) describe how you would implement...
-
Consider the heat pump described in Example 9.14. The heat pump now operates between 0.60 MPa and 1.4 MPa. Plot the vapor-compression cycle in Ts coordinates (use NIST) and determine the cycle...
-
Saturated refrigerant-134a vapor at 34C is to be condensed as it flows in a 1-cm-diameter tube at a rate of 0.1 kg/min. Determine the rate of heat transfer from the refrigerant. What would your...
-
Why do we describe qualitative analysis as an interactive process? LO6
-
It is possible for an organization to add value through overseas operations? L01
-
St. Lukes Medical Center has a single operating room that is used by local physicians to perform surgical procedures. The cost of using the operating room is accumulated by each patient procedure and...
-
please help, will give a thumbs up if correct
-
1 Carry out a PESTEL analysis of Alibaba at the time of the case. Evaluate the balance of opportunities and threats, using the same kind of figure as in Illustration 2.1. 2 Draw a basic sociogram of...
-
A labeled point has x- and y-coordinates and a string label. Provide a class Labeled- Point with a constructor LabeledPoint(int x, int y, String label) and hashCode and equals methods. Two labeled...
-
Write a method: public static void reverse that reverses the entries in a linked list. (LinkedList strings)
-
How have trade agreements helped reduce trade barriers? Do you believe these efforts will continue?
-
Recognition is a very important element of volunteer management. Do you know someone who has done amazing volunteer work for a good cause? Wouldn't it be nice to thank them with an award! Take a look...
-
What do Financial Planners do? Would you consider hiring a Financial Planner? How important are ethics when working with a financial planning professional? Explain the concept of return on...
-
Explain the specific perceptual errors you made of EACH of your teammates during the class exercise
-
Identify the company that makes the product a. Are they a large company or a small company? b. Are they a chain or a major corporation? c. Have they been around for decades or are they a new company?...
-
The Red Inn at Pismo is a 150-room hotel that caters mainly to business clients during the week and to tourists during the weekends and in the summer. Below is a table summarizing the average daily...
-
How would you report on a confidential survey in which employees rated their managers' capabilities? Both employees and managers expect to see the results. Would you give the same report to employees...
-
Use critical values to test the null hypothesis H0: 1 2 = 20 versus the alternative hypothesis H0: 1 2 20 by setting a equal to .10, .05, .01, and .001. How much evidence is there that the...
-
Explain the significance of initial sequence number in TCP.
-
What are the inherent characteristics of wireless networks that require changes in existing TCP?
-
What are the problems faced by designers of wireless TCP stacks when using link layer protocols?
-
Comfort Golf Products is considering whether to upgrade its equipment Managers are considering two options. Equipment manufactured by Stenback Inc. costs $1,000,000 and will last five years and have...
-
Weaver Corporation had the following stock issued and outstanding at January 1, Year 1: 71,000 shares of $10 par common stock. 8,500 shares of $60 par, 6 percent, noncumulative preferred stock. On...
-
Read the following case and then answer questions On 1 January 2016 a company purchased a machine at a cost of $3,000. Its useful life is estimated to be 10 years and then it has a residual value of...
Study smarter with the SolutionInn App