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

logo Practice-It logo

removePoints

Language/Type: Java Collections Sets and Maps
Author: Marty Stepp (on 2021/04/02)

Write a method called removePoints that takes a map and an index as parameters and that removes particular points from the map returning them in a set. The map this method will manipulate uses integer indexes as keys and store as values a list of points. For example, a variable called data might store the following:

        {17=[[x=3,y=4], [x=12,y=6], [x=8,y=12], [x=3,y=6]],
         42=[[x=2,y=5], [x=3,y=3], [x=1,y=5], [x=4,y=2], [x=8,y=9]], 
         308=[[x=1,y=2], [x=5,y=8], [x=4,y=4], [x=2,y=7], [x=3,y=9]]}

This map has three entries. The first entry associates the key 17 with a list of four points. The second associates the key 42 with a list of five points. The third associates 308 with a list that also has five points.

When the method is called, it will be passed the map and a key and it will return a set of points, as in:

        Set result = removePoints(data, 42);

The method should manipulate the list of points for the given index, removing any points for which the x-value is less than the y-value and returning these points in a set. After the call above, result should be:

        [[x=2,y=5], [x=1,y=5], [x=8,y=9]]

and data should store the following:

        {17=[[x=3,y=4], [x=12,y=6], [x=8,y=12], [x=3,y=6]],
         42=[[x=3,y=3], [x=4,y=2]],
         308=[[x=1,y=2], [x=5,y=8], [x=4,y=4], [x=2,y=7], [x=3,y=9]]}

Notice that the index 42 is now associated with a list of just two points (the two that weren't removed). The method should return an empty set if there are no points to remove or if the index value has no corresponding entry in the map.

Your method should construct a set to return and may construct iterators, but you are not allowed to construct other structured objects (no string, set, list, etc.).

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.