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...
-
how CVP analysis is used for decision making?
-
Financial statement analysis is a process whereby the basic financial statements are reviewed and evaluated to assess a firms financial health and/or performance. Calculating financial ratios is an...
-
The management of Advanced Alternative Power Inc. is considering two capital investment projects. The estimated net cash flows from each project are as follows: The wind turbines require an...
-
I have created a bar chart, can someone please help me to make it look more professional? Is my wording okay? Do I need to add anything else to it? The bar chart is supposed to represent a remote...
-
Ryan Ross (111-11-1112), Oscar Omega (222-22-2222), Clark Carey (333-33-3333), and Kim Kardigan (444-44-4444) are equal active members in ROCK the Ages LLC. ROCK serves as agent and manager for...
-
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)
-
Distinguish between internal audit and statutory audit.
-
For fluids with small viscosity coefficients, please explain why the viscous effect in the flow near the wall cannot be ignored.
-
How does the drag coefficient of an airfoil with ideal steady compressible flow vary with the Mach number and lift-drag ratio of the incoming flow?
-
How does the lift coefficient of an airfoil with ideal steady compressible flow vary with the Mach number of the incoming flow and why?
-
Please indicate the conditions of the separation point in the boundary layer. Why does the resistance of the object flow around increase significantly after the boundary layer is separated?
-
Why is the velocity distribution in the boundary layer near the object surface unstable under the reverse pressure gradient, and there is a velocity inflection point in the boundary layer?
-
What is the relationship between training and the service desk?
-
1. Which of the four major types of information systems do you think is the most valuable to an organization? 2. How do you critically associate the ideas of business agility and business efficiency...
-
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?
-
The NFP receives a donation stipulating that it will be expended on building low-cost housing in urban areas within the next years. 2. A donor makes a verifiable unconditional promise to the NFP to...
-
Write about the economic outlook of Turkey with at least 5 references. Include your own expectations regarding the Turkish financial market. Then, write about your investment decisions ( including...
-
You buy a pair of jeans at a store, at a sale price of $100. The store paid $40 to its supplier for the pair of jeans. What is the net income for the store? Select an answer: 100 70 60 40
Study smarter with the SolutionInn App