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: 0% (2 reviews)
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...
-
In 2020, Nitai (age 40) contributes 10 percent of his $100,000 annual salary to a Roth 401(k) account sponsored by his employer, AY Inc. AY Inc. matches employee contributions to the employees...
-
A U.S. Treasury bond pays a 7% coupon on January 7 and July 7. How much interest accrues per \($100\) of principal to the bond holder between July 7, 2013, and August 9, 2013? How would your answer...
-
Elm Company had total assets of $640,000 in 2012, $680,000 in 2013, and $760,000 in 2014. In 2013, Elm had net income of $77,112 on revenues of $1,224,000. In 2014, it had net income of $98,952 on...
-
In its simplest form, profit is: Fixed costs - Depreciation - Interest = Profit Sales/Turnover - Variable costs/Used goods = Profit (Actual retail price - cost)/ Actual retail price = Profit (Price -...
-
On January 1, Year 2, PAT Ltd. acquired 90% of SAT Inc. when SAT's retained earnings were $1,000,000. There was no acquisition differential. PAT accounts for its investment under the cost method. SAT...
-
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)
-
More lenses Object O stands on the central axis of a thin symmetric lens. For this situation, each problem in Table 34-8 refers to (a) The lens type, converging (C) or diverging (D), (b) The focal...
-
List advantages and disadvantages of the Uniform Construction Index (UCI) coding system.
-
If your credit cards APR is 24% compounded daily, what is the effective annual interest rate that you are paying?
-
Which business enterprises pay untaxed profits to owners/partners who then pay the taxes on their individual returns?
-
What is meant by contractor O & P? Give three components that are considered in the O & P.
-
Find the yearly depreciation of a $100,000 five-year class concrete mixer, first assuming that it is new when purchased, and then that it is purchased as used equipment.
-
The global recession forced thousands of firms into bankruptcy. Does this fact alone confirm that external factors are more important than internal factors in strategic planning? Discuss.
-
1) The government decided to reduce taxes on fast-food to increase revenue. The government assumes that fast-food products have a) An inelastic demand b) An elastic demand c) A demand curve that is...
-
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?
-
The activation energy of an isomerization reaction which obeys 1st-order kinetics is known to be 100 kJ/mol and the pre-exponential of reaction is 1.666 x 103 cm/s. When the isomerization reaction is...
-
1. How did you ML and MP trees compare to each other in terms of relationships of coronaviruses? What about in terms of support from bootstrapping (both used bootstrapping method)? 2. Which method...
-
What is a strength of this study? The Present Study The primary purpose of the present study, then, was to further investigate to what extent different note-taking mediums (laptop, longhand) and...
Study smarter with the SolutionInn App