【问题标题】:Check whether a map contains all contents of another map检查一个地图是否包含另一个地图的所有内容
【发布时间】:2017-08-30 05:36:28
【问题描述】:

我正在尝试检查一张地图是否包含另一张地图的所有内容。例如,我有一个mapA,它是一个Map<String, List<String>>,元素是:

"1" -> ["a","b"]
"2" -> ["c","d"]

另一个mapB,也是一个Map<String, List<String>>,元素是:

"1" -> ["a"]
"2" -> ["c","d"],

我想创建一个函数 compare(mapA, mapB) 在这种情况下将返回 false。

最好的方法是什么?

【问题讨论】:

  • 只是为了你未来的自我理智,不要称之为比较。你不是在比较。将其命名为 containsAllsubsumes

标签: java hashmap contains


【解决方案1】:

在您的 compare(mapA, mapB) 方法中,您可以简单地使用:

return mapA.entrySet().containsAll(mapB.entrySet());

【讨论】:

  • 但这也会检查值吗?如果不是,那么检查键和值的更好方法是什么?
  • @theprogrammer 我不知道为什么我从来没有回复你的评论,但是是的,这将检查mapAmapB 中的所有条目是否存在。条目由键和值组成,条目相等由键相等和值相等来确定。
【解决方案2】:

@Jacob G 提供的答案不适用于您的情况。只有在MapA 中有一个额外的(键,值)对时,它才会起作用。喜欢

MapA = {"1" -> ["a","b"] "2" -> ["c","d"] } 

MapB = {"1" -> ["a","b"]  }. 

你需要的是这个:

boolean isStrictlyDominate(LinkedHashMap<Integer, HashSet<Integer>> firstMap, LinkedHashMap<Integer, HashSet<Integer>> secondMap){
    for (Map.Entry<Integer, HashSet<Integer>> item : secondMap.entrySet()) {
        int secondMapKey = item.getKey();
        if(firstMap.containsKey(secondMapKey)) {
            HashSet<Integer> secondMapValue = item.getValue();
            HashSet<Integer> firstMapValue = firstMap.get(secondMapKey) ;
            if(!firstMapValue.containsAll(secondMapValue)) {
                return false;
            }

        }
    }
    return !firstMap.equals(secondMap);
}

(如果您不想检查严格控制,那么只需 return 最后为真 return 声明)

【讨论】:

    【解决方案3】:

    试试这个代码:

    Assert.assertTrue(currentMap.entrySet().containsAll(expectedMap.entrySet()));

    【讨论】:

      【解决方案4】:

      你可以试试这个。

      static boolean compare(Map<String, List<String>> mapA, Map<String, List<String>> mapB){
              return mapA.entrySet().containsAll(mapB.entrySet());
          }
      

      假设,提供的数据是这样的:

                  Map<String, List<String>> mapA = new HashMap<>();
                  Map<String, List<String>> mapB = new HashMap<>();
      
                  mapA.put("1", Arrays.asList("a","b"));
                  mapA.put("2", Arrays.asList("c","d"));
      
                  mapB.put("1", Arrays.asList("a"));
                  mapB.put("2", Arrays.asList("c", "d"));
      
                  System.out.println(compare(mapA, mapB));
      

      在这种情况下,compare(mapA, mapB) 方法将返回 false。 但是假设提供的数据是这样的:

                  Map<String, List<String>> mapA = new HashMap<>();
                  Map<String, List<String>> mapB = new HashMap<>();
      
                  mapA.put("1", Arrays.asList("a","b"));
                  mapA.put("2", Arrays.asList("c","d"));
      
                  mapB.put("1", Arrays.asList("a", "b"));
                  mapB.put("2", Arrays.asList("c", "d"));
      
                  System.out.println(compare(mapA, mapB));
      

      在这种情况下,我编写的 compare(mapA, mapB) 方法将返回 true。

      compare(mapA, mapB) 方法基本上是检查 mapA 和 mapB 中的所有条目,如果相同则返回 yes,否则返回 false;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 2021-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-30
        • 2014-03-15
        相关资源
        最近更新 更多