簡體   English   中英

在地圖中查找匹配的鍵和值

[英]Finding Matching Keys and Values in a Map

在這個問題中,我必須在兩個映射中找到所有匹配的鍵/值映射,然后將其返回到新映射中,但是我遇到了一些問題。 我的想法是從兩個映射圖中找到所有匹配的鍵,然后使用這些鍵將其引用為值。 如果值匹配,我會將鍵/值放入映射中。 我試圖找出為什么它只是添加了所有共同的鑰匙。 僅當其對應值也匹配時才添加這些鍵。 謝謝。

提示:

編寫一個方法相交,將兩個字符串映射為整數的整數作為參數,然后返回一個新映射,其內容為兩者的交集。 此處將兩個地圖的交集定義為兩個地圖中都存在的一組鍵和值。 因此,如果某些鍵K在第一個和第二個映射中都映射到值V,則將其包括在結果中。 如果在兩個映射中都不存在K作為鍵,或者在兩個映射中K均未映射為相同的值V,請從結果中排除該對。 例如,考慮以下兩個地圖:

{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}

在前面的地圖上調用方法將返回以下新的地圖(鍵/值對的順序無關緊要):

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

我的代碼:

// 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;
}

您正在將所有值從第一個傳遞到輸出,方法是將其傳遞給構造函數。

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

您無需創建另一個Set,keySet()方法將返回set,因此不需要以下幾行。

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
}

這是正確的實現。

// 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;
}

對於此練習,更簡單的解決方案是跳過迭代器並使用for循環,如下所示。 對於map1中的每個名稱,我們檢查它是否存在於map2中,以及值是否匹配。 然后將K和V添加到新地圖:

public static Map intersect(Map<String, Integer> map1, Map<String, Integer> map2){
    Map<String, Integer> newMap = new HashMap<>();
    for (String name : map1.keySet()){
        if(map2.containsKey(name) && map1.get(name).equals(map2.get(name))){
            newMap.put(name, map1.get(name));
        }
    }
    return newMap;        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM