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...
-
Prove the given statement about subsets A and B of R n , or provide the required example in R 2 . A proof for an exercise may use results from earlier exercises (as well as theorems already available...
-
Many corporations finance at least a part of their operations and asset purchases using debt, principally because the cost of debt financing is cheaper than equity financing. Moreover, some firms are...
-
Prepare journal entries to record the following merchandising transactions of Sheng Company, which applies the perpetual inventory system. Aug. 1 Purchased merchandise from Arotek Company for $ 7,500...
-
ll . Suppose you bought 1 0 bonds with coupon rate of 5 % one year ago for $ 9 8 0 per bond. The bond sells for $ 1 , 0 1 0 today. ( 1 ) Assuming a $ 1 , 0 0 0 face value, what was your total dollar...
-
Solve y" + y = 1 - t 2 / 2 ? if 0 < t < and 0 if t ?? ??; here, y(0) = 0, y' (0) = 0. This models an undamped system on which a force F acts during some interval of time (see Fig. 59), for instance,...
-
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)
-
A plant asset was purchased on January 1 for $100,000 with an estimated salvage value of $20,000 at the end of its useful life. The current year's Depreciation Expense is $10,000 calculated on the...
-
What is the linearized equation for the velocity potential function of compressible potential flow? Please describe the linearization conditions.
-
Derive a differential equations that describes the motion of an ideal compressible fluid.
-
Please write down the vorticity diffusion equation in the boundary layer.
-
Write down the displacement thickness of the boundary layer, the thickness of the momentum loss, and the thickness of the energy loss. Explain the physical meaning.
-
Please explain the basic assumptions of the boundary layer approximation.
-
What is the service desk? What does it do?
-
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...
-
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?
-
A company purchased an asset for $3,800,000 that will be used in a 3-year project. The asset is in the 3-year MACRS class. The depreciation percentage each year is 33.33 percent, 44.45 percent, and...
-
James has a mortgage of $92,500 at 4% for 15 years. The property taxes are $3,900 per year, and the hazard insurance premium is $764.50 per year. Find the monthly PITI payment (in $). (Round your...
-
Complete the yellow cells. Year 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 Discounting of user benefits: Discount Project costs User benefits Other Benefits Rate Journey CO Safety...
Study smarter with the SolutionInn App