Question: Add a method called reportMap that accepts a set of Strings as its parameter and returns the largest string set. Use a map as auxiliary
Add a method called "reportMap" that accepts a set of Strings as its parameter and returns the largest string set. Use a map as auxiliary storage.
Build a map that maps strings by its length. For example, "a" will paired with 1, and "Hello" will be paired with 5.
Create a Map
The largest Set of words in the Mobydick file has the length 7 and may looks as follows (If you use TreeSet) : ["'Cross, "'Nay,', "'Shall, "'Twill, "'Where, "'Who's, "Astern, "Attend, "Avast!, " ......
import java.io.*; import java.util.*; public class Mobydick { // Add reportMap here // DO NOT MODIFY the code below //////////////////////////// public static void main (String [] args) { try{ Set words = new HashSet(); Scanner input = new Scanner (new File ("mobydick.txt")); while (input.hasNext()) { words.add(input.next()); } System.out.println (reportMap(words)); } catch (FileNotFoundException e) { System.out.println (e); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
