Java 比较相等值和相同键集的两个哈希图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20969211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:38:04  来源:igfitidea点击:

Comparing two hashmaps for equal values and same key sets?

javacollectionshashmapcomparison

提问by membersound

How can I best compare two HashMaps, if I want to find out if none of them contains different keys than the other, and if the values of that keys match each other.

我怎样才能最好地比较两个HashMaps,如果我想知道它们中是否没有一个包含与另一个不同的键,以及这些键的值是否相互匹配。

Map<objA, objB> mapA = new HashMap<objA, objB>();
mapA.put("A", "1");
mapA.put("B", "2");

Map<objA, objB> mapB = new HashMap<objA, objB>();
mapB.put("D", "4");
mapB.put("A", "1");

When comparing A with B, it should fail due to different keys B and D.

当比较 A 和 B 时,由于 B 和 D 不同的键,它应该会失败。

How could I best compare non-sorted hashmaps?

我怎样才能最好地比较未排序的哈希图?

采纳答案by Narendra Pathai

Make an equalscheck on the keySet()of both HashMaps.

做一个equals支票上keySet()两者HashMap秒。

NOTE:

笔记:

If your Mapcontains Stringkeys then it is no problem, but if your Map contains objAtype keys then you need to make sure that your class objAimplements equals().

如果您的Map包含String键,那没问题,但如果您的 Map 包含objA类型键,那么您需要确保您的类objA实现equals().

回答by user2336315

Simply use :

只需使用:

mapA.equals(mapB);

mapA.equals(mapB);

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the samemappings

比较指定对象与此映射是否相等。如果给定的对象也是一个映射并且这两个映射表示相同的映射,则返回 true

回答by Antonio Ortells

Compare every key in mapB against the counterpart in mapA. Then check if there is any key in mapA not existing in mapB

将 mapB 中的每个键与 mapA 中的对应键进行比较。然后检查mapA中是否有任何键在mapB中不存在

public boolean mapsAreEqual(Map<String, String> mapA, Map<String, String> mapB) {

    try{
        for (String k : mapB.keySet())
        {
            if (!mapA.get(k).equals(mapB.get(k))) {
                return false;
            }
        } 
        for (String y : mapA.keySet())
        {
            if (!mapB.containsKey(y)) {
                return false;
            }
        } 
    } catch (NullPointerException np) {
        return false;
    }
    return true;
}

回答by SanA

public boolean compareMap(Map<String, String> map1, Map<String, String> map2) {

    if (map1 == null || map2 == null)
        return false;

    for (String ch1 : map1.keySet()) {
        if (!map1.get(ch1).equalsIgnoreCase(map2.get(ch1)))
            return false;

    }
    for (String ch2 : map2.keySet()) {
        if (!map2.get(ch2).equalsIgnoreCase(map1.get(ch2)))
            return false;

    }

    return true;
}

回答by Haroon Rawat

/* JAVA 8 using streams*/
   public static void main(String args[])
    {
        Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
        map.put(100, true);
        map.put(1011, false);
        map.put(1022, false);

        Map<Integer, Boolean> map1 = new HashMap<Integer, Boolean>();
        map1.put(100, false);
        map1.put(101, false);
        map1.put(102, false);

        boolean b = map.entrySet().stream().filter(value -> map1.entrySet().stream().anyMatch(value1 -> (value1.getKey() == value.getKey() && value1.getValue() == value.getValue()))).findAny().isPresent();
        System.out.println(b);
    }

回答by Swati

if you have two maps lets say map1 and map2 then using java8 Streams,we can compare maps using code below.But it is recommended to use equals rather then != or ==

如果你有两个地图让我们说 map1 和 map2 然后使用 java8 Streams,我们可以使用下面的代码比较地图。但建议使用 equals 而不是 != 或 ==

boolean b = map1.entrySet().stream().filter(value -> 
            map2.entrySet().stream().anyMatch(value1 -> 
            (value1.getKey().equals(value.getKey()) && 
  value1.getValue().equals(value.getValue())))).findAny().isPresent();   


System.out.println("comparison  "+b);