12: Map Finger Exercises

Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.


A. (2 points) Suppose you had a Map<Integer, List<String>> . Write a method static int count(Map<Integer, List<String>> map, String string) that returns the number of times a given String appears in the map.

B. (1 point) Suppose you had a Map<String, Integer> called hap that contains key "ex" with value 2. Draw a diagram representing how the Map stores "ex".

C. (1 point) Suppose you had a Map<Integer, Integer>. Write a method static int sumProducts(Map<Integer, Integer> map)that sums the products of each key with its value. For example, if a Map contains entries <1, 3> (that is, the key 1 associated with the value 3) and <2, 4> it should return (1*3) + (2*4).

D. (1 point) Suppose you wanted to create a Map that used Strings as keys. The values associted with these String keys are also of type Map; in this second map, the keys are Integers and the values are Characters . Write the type declaration for such a Map.