Java – Finding Matching Keys and Values in a Map

iteratorjavamapset

In this problem, I have to find all matching key/value mappings within two maps and then return it into a new map, but I am running into some problems. My idea is to find all matching keys from the two maps, then use these keys to reference it to the values. If the values matched, I would put the key/value into the map. I am trying to find out why it just adds all the keys that are in common; it only add those keys if its corresponding values match too. Thanks.

The prompt:

Write a method intersect that takes two Maps of strings to integers as parameters and that returns a new map whose contents are the intersection of the two. The intersection of two maps is defined here as the set of keys and values that exist in both maps. So if some key K maps to value V in both the first and second map, include it in your result. If K does not exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair from your result. For example, consider the following two maps:

{Janet=87, Logan=62, Whitaker=46, Alyssa=100, Stefanie=80, Jeff=88, Kim=52, Sylvia=95}
{Logan=62, Kim=52, Whitaker=52, Jeff=88, Stefanie=80, Brian=60, Lisa=83, Sylvia=87}

Calling your method on the preceding maps would return the following new map (the order of the key/value pairs does not matter):

{Logan=62, Stefanie=80, Jeff=88, Kim=52}

My code:

// we need to store the keys, then get the values in common, then put the key/map into map
public static Map<String, Integer> intersect(Map<String, Integer> first, Map<String, Integer> second) {
    Map<String, Integer> output = new HashMap<String, Integer>(first); // combined output
    Set<String> keyFirst = new HashSet<String>(); // stores all the keys for first
    for (String key: first.keySet()) { // goes through each key in input
        keyFirst.add(key); // adds all keys from first into keyFirst
    }

    // goes through each key in common and checks to see if they reference to the same value
    Iterator<String> keyFirstItr = keyFirst.iterator();
    while (keyFirstItr.hasNext()) {
        String keyTemp = keyFirstItr.next();
        if (first.get(keyTemp) == second.get(keyTemp)) { // If same key, same value mapped
            output.put(keyTemp, first.get(keyTemp)); // add key value to map
        }
    }
    return output;
}

Best Answer

You are putting all the values from first to output by passing it to the constructor.

Map<String, Integer> output = new HashMap<String, Integer>(first); // you are passing first to the constructor.

You don't need to create another Set, keySet() method returns set so the below lines not required.

Set<String> keyFirst = new HashSet<String>(); // stores all the keys for first
    for (String key: first.keySet()) { // goes through each key in input
        keyFirst.add(key); // adds all keys from first into keyFirst
}

Here's the correct implemetataion.

// we need to store the keys, then get the values in common, then put the key/map into map
public static Map<String, Integer> intersect(Map<String, Integer> first, Map<String, Integer> second) {
    Map<String, Integer> output = new HashMap<String, Integer>(); // combined output

    // goes through each key in common and checks to see if they reference to the same value    
    Iterator<String> keyFirstItr = first.keySet().iterator();
    while (keyFirstItr.hasNext()) {
        String keyTemp = keyFirstItr.next();
        if (first.get(keyTemp).equals(second.get(keyTemp))) { // If same key, same value mapped
            output.put(keyTemp, first.get(keyTemp)); // add key value to map
        }
    }
    return output;
}