logo Practice-It logo


Language/Type: Java Collections Sets and Maps
Author: Marty Stepp (on 2020/10/22)

Write a method called convert that takes as a parameter a set of strings representing phone numbers and that returns a map of strings to sets of strings that represent the same phone numbers split into exchange/suffix. In particular, the phone numbers in the set passed to the method will each include a 3-digit exchange followed by a dash followed by a four-digit suffix, as in:

[493-3923, 723-9278, 384-1917, 555-1795, 384-4923, 555-4923, 555-1212, 723-9823]

The method should split each string into the 3-digit exchanges that come before the dash and the suffixes that come after. The method should construct a map in which the keys are the 3-digit exchanges. Each such exchange will be mapped to a set of 4-digit suffixes. For the set of phone numbers above, the following map would be constructed:

{384=[1917, 4923], 493=[3923], 555=[1212, 1795, 4923], 723=[9278, 9823]}

Notice, for example, that three of the phone numbers in the original set began with "555". In the map, the key "555" maps to a set of three elements (the three suffixes that came after "555-").

Your method should construct the new map and each of the sets contained in the map. Recall that the String class has a substring method that takes a starting index (inclusive) and a stopping index (exclusive). For example:

"Australia".substring(1, 5) returns "ustr"

The keys of the new map should be ordered by the 3-digit exchanges and each set should be ordered by the 4-digit suffixes in that set.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.