This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

indexMap

Language/Type: Java Collections
Author: Marty Stepp (on 2020/11/06)

Write a method called indexMap that takes a list of strings as a parameter and that returns a map that associates each string from the list to a list of indexes at which that string occurs. For example, if a variable called list stores the following:

        [to, be, or, not, to, be]

then the call indexMap(list) should return the following map:

        {be=[1, 5], not=[3], or=[2], to=[0, 4]}

Notice that each string from the original list maps to a list of indexes. For example, the string "to" appeared at index 0 and 4 in the original list, so it maps to a list with the values 0 and 4. The map returned by your method should be ordered alphabetically and the lists that each string maps to should have indexes in increasing order, as in the example.

Your method should construct the new map and each of the lists contained in the map and can construct iterators but should otherwise not construct any new data structures. It should also not modify the list of words passed as a parameter and it should be reasonably efficient.

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.