Comparing two hashmaps for equal values and same key sets?

后端 未结 6 952
北荒
北荒 2020-12-03 02:18

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 oth

6条回答
  •  有刺的猬
    2020-12-03 03:04

    public boolean compareMap(Map map1, Map 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;
    }
    

提交回复
热议问题