logo CodeStepByStep logo


Language/Type: Java collection mystery collections

Write the output that is printed when the given method below is passed each of the following pairs of maps as its parameter. Recall that maps print in a {key1=value1, key2=value2, ..., keyN=valueN} format. Your answer should display the right keys and values in the order they are added to the resulting map.

public HashMap<String, String> collectionMystery2(HashMap<String, Integer> map1, HashMap<Integer, String> map2) {
    HashMap<String, String> result = new HashMap<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}
map1={five=105, four=104, one=101, six=106, three=103, two=102}, map2={99=uno, 101=dos, 103=tres, 105=cuatro}
map1={a=42, b=9, c=7, d=15, e=11, f=24, g=7}, map2={1=four, 3=score, 5=and, 7=seven, 9=years, 11=ago}

You must log in before you can solve this problem.

Log In

Need help?

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

Is there a problem? Contact a site administrator.

©, all rights reserved.