简体   繁体   English

比较超过 3 个 HashMap 的 Key

[英]Compare Keys of more than 3 HashMaps

I want to compare the keys of four hashmaps.我想比较四个哈希图的键。 All of the keys in the Hashmaps are equal. Hashmap 中的所有键都是相等的。 But I would like to evaluate each hashmap to compare the keys of all four hashmaps.但我想评估每个 hashmap 以比较所有四个哈希图的键。 In the code below, I am using 4 nested loops and then using a logical AND.在下面的代码中,我使用 4 个嵌套循环,然后使用逻辑 AND。 If true , I want to add the Values to an Array List.如果为true ,我想将值添加到数组列表中。 Is this possible?这可能吗?

I have seen some code on the forum, but found nothing where we can compare 4 hashmaps.我在论坛上看到了一些代码,但没有找到可以比较 4 个哈希图的地方。 So do I need to add the Values to the ArrayList and not the Keys?那么我是否需要将值添加到 ArrayList 而不是键?

public static Map<String, Boolean> areEqualKeyValues(Map<String, String> firstMap, Map<String, String> secondMap,Map<String, String> thirdMap, Map<String, String> fourthMap) {

        ArrayList<String> reporValues = new ArrayList<>();
        HashMap<String, String> filteredMap = new HashMap<>();

        for (Map.Entry<String, String> entry1 : firstMap.entrySet()) {
            for (Map.Entry<String, String> entry2 : secondMap.entrySet()) {
                for (Map.Entry<String, String> entry3 : thirdMap.entrySet()) {
                    for (Map.Entry<String, String> entry4 : fourthMap.entrySet()) {
                            if (entry1.getKey().equals(entry2.getKey())
                                    && entry2.equals(entry3.getKey())
                                    && entry1.equals(entry3.getKey())
                                    && entry3.equals(entry4.getKey())) {
                                reporValues.add(entry1.getValue() + entry2.getValue() + entry3.getValue() + entry4.getValue());
                            }
                        }
                    }
                }
                filteredMap.put(entry1.getKey(), reporValues.toString());
            }

Your code has some problems:您的代码有一些问题:

  1. The return type is wrong.返回类型错误。
  2. You need to compare a key with a key whereas you have compared some keys with the entry itself.您需要将键与键进行比较,而您已将某些键与条目本身进行了比较。
  3. You haven't returned anything from the function.您没有从 function 返回任何东西。

Given below is the corrected function with a demo code:下面给出的是带有演示代码的更正后的 function:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(final String[] args) {
        // Test
        Map<String, String> firstMap = Map.of("a", "a1", "b", "b-first_", "c", "c1", "d", "d1");
        Map<String, String> secondMap = Map.of("x", "z1", "b", "b-second_", "y", "y1", "z", "z1");
        Map<String, String> thirdMap = Map.of("b", "b-third_", "m", "m1", "n", "n1");
        Map<String, String> fourthMap = Map.of("a", "a1", "p", "p1", "b", "b-fourth_");
        Map<String, String> filteredMap = new HashMap<>();
        List<String> reporValues = new ArrayList<>();
        System.out.println(areEqualKeyValues(firstMap, secondMap, thirdMap, fourthMap, filteredMap, reporValues));
        System.out.println(filteredMap);
        System.out.println(reporValues);
    }

    public static boolean areEqualKeyValues(Map<String, String> firstMap, Map<String, String> secondMap,
            Map<String, String> thirdMap, Map<String, String> fourthMap, Map<String, String> filteredMap,
            List<String> reporValues) {

        for (Map.Entry<String, String> entry1 : firstMap.entrySet()) {
            for (Map.Entry<String, String> entry2 : secondMap.entrySet()) {
                for (Map.Entry<String, String> entry3 : thirdMap.entrySet()) {
                    for (Map.Entry<String, String> entry4 : fourthMap.entrySet()) {
                        if (entry1.getKey().equals(entry2.getKey()) && entry2.getKey().equals(entry3.getKey())
                                && entry3.getKey().equals(entry4.getKey()) && entry4.getKey().equals(entry1.getKey())) {
                            reporValues
                                    .add(entry1.getValue() + entry2.getValue() + entry3.getValue() + entry4.getValue());
                            filteredMap.put(entry1.getKey(), reporValues.toString());
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}

Output: Output:

true
{b=[b-first_b-second_b-third_b-fourth_]}
[b-first_b-second_b-third_b-fourth_]

Try this.尝试这个。

public static Map<String, String> areEqualKeyValues(Map<String, String>... maps) {
    if (maps.length <= 0)
        throw new IllegalArgumentException("maps");
    Set<String> keys = new HashSet<>();
    for (Map<String, String> map : maps)
        keys.addAll(map.keySet());
    Map<String, String> filteredMap = new HashMap<>();
    for (String key : keys) {
        StringBuilder sb = new StringBuilder();
        for (Map<String, String> map : maps)
            sb.append(map.get(key));
        filteredMap.put(key, sb.toString());
    }
    return filteredMap;
}

You should not iterate over the entrySet but use the keySet instead.您不应该迭代entrySet而是使用keySet
Then you can just use the retainAll of the first keySet repeatedly to only retain the objects that are in all the sets.然后你可以重复使用第一个keySetretainAll来只保留所有集合中的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM