Read all words from a file and add them to a map whose keys are word lengths
Question:
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 order by the length of their entries.
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: 33% (3 reviews)
The task is to read words from a file and compile them in a Map where the keys are word lengths and the values are strings made up of commaseparated w...View the full answer
Answered By
Somshukla Chakraborty
I have a teaching experience of more than 4 years by now in diverse subjects like History,Geography,Political Science,Sociology,Business Enterprise,Economics,Environmental Management etc.I teach students from classes 9-12 and undergraduate students.I boards I handle are IB,IGCSE, state boards,ICSE, CBSE.I am passionate about teaching.Full satisfaction of the students is my main goal.
I have completed my graduation and master's in history from Jadavpur University Kolkata,India in 2012 and I have completed my B.Ed from the same University in 2013. I have taught in a reputed school of Kolkata (subjects-History,Geography,Civics,Political Science) from 2014-2016.I worked as a guest lecturer of history in a college of Kolkata for 2 years teaching students of 1st ,2nd and 3rd year. I taught Ancient and Modern Indian history there.I have taught in another school in Mohali,Punjab teaching students from classes 9-12.Presently I am working as an online tutor with concept tutors,Bangalore,India(Carve Niche Pvt.Ltd.) for the last 1year and also have been appointed as an online history tutor by Course Hero(California,U.S) and Vidyalai.com(Chennai,India).
4.00+
2+ Reviews
10+ 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 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...
-
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...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
On July 31, 2023, Magnuss Muffins paid $18,000 for business insurance for the next year. Record the entries for the purchase of the insurance by recording it as an expense and then making a year-end...
-
The average atmospheric pressure in Spokane, Washington (elevation =2350 ft), is 13.5 psia, and the average winter temperature is 36.5F. The pressurization test of a 9-ft-high, 3000-ft2 older home...
-
What types of data will you need to retain and file while you are undertaking qualitative research? LO6
-
What are the conditions under which contagion spreads from country to country? L01
-
Modern Fun Corporation sells electronic games. Its five salespersons are currently being paid fixed salaries of $30,000 each; however, the sales manager has suggested that it might be more profitable...
-
Fund financial statements: The City of Manchester, New Hampshire, has only one activity, its school system. The school system is accounted for within the general fund. For convenience, assume that at...
-
You are the senior acquisitions associate at a New York-based private equity fund with a core investment focus. You have reached an agreement to purchase a well-located, 185,000 square foot office...
-
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)
-
This cases focuses on the issue of performance. As discussed in the text, clear and effective goals help improve employee performance, reduce role stress conflict and ambiguity, and improve the...
-
Reflect on your semester. How do you plan onmeasuringyour professionalgrowth in the future? What were the most challenging topics to you? What topics felt more intuitive/easy? How do you plan on...
-
Aside from shareholders, who do you believe is the second stakeholder in whose interests the company should be concerned? Justify your response What will you do to ensure the company's success...
-
a) What CSR did your organization do - how did it improve your organization's image? b) If your organization did not do any CSR, as the boss, what CSR activities would you suggest doing and why?
-
Do you believe NIL promotes "love of the game," or does it make college sports more about money and business? What are the most significant positive and negative effects of NIL, in your opinion? What...
-
Even well-managed organizations do not always work as efficiently and effectively as management would like. At Hewlett-Packard (HP), billions of dollars of product are being shipped - from computers...
-
You and a classmate are helping Linda Moreno prepare her report on Electrovision's travel and entertainment costs. This time, however, the report is to be informational rather than analytical, so it...
-
Flicker, Inc., a closely held corporation, acquired a passive activity this year. Gross income from operations of the activity was $160,000. Operating expenses, not including depreciation, were...
-
What is DHCP? How does DHCP support dynamic address allocation?
-
With suitable examples, explain the differences between connection-oriented and connectionless protocols.
-
What are the disadvantages of using wireline TCP over wireless networks?
-
In 2019, Sunland Company had a break-even point of $388,000 based on a selling price of $5 per unit and fixed costs of $155,200. In 2020, the selling price and the variable costs per unit did not...
-
11. String Conversion Given a binary string consisting of characters '0's and '1', the following operation can be performed it: Choose two adjacent characters, and replace both the characters with...
-
Consider the table shown below to answer the question posed in part a. Parts b and c are independent of the given table. Callaway Golf (ELY) Alaska Air Group (ALK) Yum! Brands (YUM) Caterpillar...
Study smarter with the SolutionInn App