logo Practice-It logo

BJP4 Self-Check 11.21: mapMystery3

Language/Type: Java Collection mystery Sets and Maps
Author: Marty Stepp (on 2016/09/08)

Write the output that is printed when the given method below is passed each of the following pairs of maps as its parameter. Assume that each parameter map stores its key/value pairs in exactly the order shown, and that is the order in which a for-each loop would examine them. Recall that maps print in a {key1=value1, key2=value2, ..., keyN=valueN} format. Your answer should display the right keys and values in the right order.

public Map<String, String> mystery(Map<String, Integer> map1, Map<Integer, String> map2) {
    Map<String, String> result = new TreeMap<String, String>();
    for (String s1 : map1.keySet()) {
        if (map2.containsKey(map1.get(s1))) {
            result.put(s1, map2.get(map1.get(s1)));
        }
    }
    return result;
}

// map1={bar=1, baz=2, foo=3, mumble=4}
// map2={1=earth, 2=wind, 3=air, 4=fire}

// map3={five=105, four=104, one=101, six=106, three=103, two=102}
// map4={99=uno, 101=dos, 103=tres, 105=quatro}

// map5={a=42, b=9, c=7, d=15, e=11, f=24, g=7}
// map6={1=four, 3=score, 5=and, 7=seven, 9=years, 11=ago}
mystery(map1, map2)
mystery(map3, map4)
mystery(map5, map6)

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.