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% (QA)
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...
-
WAR (We Are Rich) has been in business since 1987. WAR is an accrual-method sole proprietorship that deals in the manufacturing and wholesaling of various types of golf equipment. Hack & Hack CPAs...
-
A company wishes to hedge its exposure to a new fuel whose price changes have a 0.6 correlation with gasoline futures price changes. The company will lose $1 million for each 1 cent increase in the...
-
Calgary Corporation produces a liquid solvent in two departments: Mixing and Finishing. Assume that Calgary Corporation provides you with the following information for finishing operations for...
-
3. (L)Explain the four terms in the equation below, and why this equation has to be true by definition. Explain the assumption that allows us to use this equation to determine the agricultural...
-
You are part of the engagement team for the audit of Suzuki Manufacturing for the year ended December 31, 2019, and are responsible for auditing the acquisition cycle. Download the Excel file for the...
-
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)
-
Assume that the probabilities of demand in Problem S1-17 are no longer valid; the decision situation is now one without probabilities. Determine the best number of cards to stock using the following...
-
Refer to the transactions for Neveranerror Inc. in Problem 3-7. Required 1. Prepare journal entries on the books of Neveranerror to record the transactions entered into during the month. Ignore...
-
Some people feel that lump-sum contracts can foster a more contentious work environment than any of the other types of contracts discussed in this chapter. What is the basis for this perception? Do...
-
What is a jurisdictional dispute? Why does this kind of dispute present no problem in District 50 locals?
-
Describes two stages in the battle for supremacy in the use of software for the mobile phone market. If attack is the best form of defence, then the mobile phone companies are striking out to repel...
-
Describe briefly two advantages and two disadvantages of a corporate form of business organization as compared to a partnership.
-
Describe the process of performing an external audit in an organization doing strategic planning for the first time.
-
Open Text Corporation provides a suite of business information software products. Exhibit 10-9 contains Note 10 from the companys 2013 annual report detailing long-term debt. Required: a. Open Text...
-
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?
-
Global steel prices have a year-over-year inflationary rate increase of 12.4 percent. Tube Fab purchased $700,000 of a particular carbon steel during the year just ended right now, and they intend to...
-
American Siesta Inc. (ASI) bonds have a coupon rate of 8% and pay coupons semiannually. The bonds have 10 years remaining to maturity and are selling for $875.38. What is the capital gain yield of...
-
The balance sheet of XYZ Corporation is shown below. XYZ Corporation ASSETS Total Assets December 31 LIABILITIES Accounts Payable $3,000,000 Bond $7,000,000 Preferred Stock $3,000,000 Common Stock...
Study smarter with the SolutionInn App