logo CodeStepByStep logo

collectionMystery3

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. You should assume that when looping over the map, it loops over the keys in the order that they are declared below.

public static void collectionMystery3(HashMap<String, String> map) {
    ArrayList<String> list = new ArrayList<String>();
    for (String key : map.keySet()) {
        if (map.get(key).length() > key.length()) {
            list.add(map.get(key));
        } else {
            list.add(0, key);
            list.remove(map.get(key));
        }
    }
    System.out.println(list);
}
{horse=cow, cow=horse, dog=cat, ok=yo}
{bye=hello, bird=dog, hi=hello, hyena=apple, fruit=meat}
{a=b, c=d, e=a, ff=a, gg=c, hhh=ff}

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.