Java:Map包含等于或小于另一个Map的内容

huangapple go评论82阅读模式
英文:

Java: Map Contains Equals to or Less Than of Another Map

问题

How do I check if 1 subset contains less than or equal to of another subset? SMap and TMap below.

In this example, doing equation returns false.

sMap.entrySet().containsAll(tMap.entrySet())

I believe it returns false since it is trying to equal the count of A, B, C. {1, 2, 2} does not equal {1, 1, 1}. However, it should be true since {1, 1, 1} <= {1, 2, 2} for each element A, B, C.

Is there an easy method to do a contains less than or equal to? Otherwise, I will just write a manual for loop with a count.

英文:

How do I check if 1 subset contains less than or equal to of another subset? SMap and TMap below.

In this example, doing equation returns false.

sMap.entrySet().containsAll(tMap.entrySet())

I believe it returns false, since it is trying equal the count of A,B,C. {1,2,2} does Not equal {1,1,1}. However, it should be true since {1,1,1} <= {1,2,2} for each element A,B,C.

Is there an easy method to doing a contains less than or equal to? Otherwise I will just write a manual for loop with count.

Java:Map包含等于或小于另一个Map的内容

https://stackoverflow.com/questions/43217055/check-whether-a-map-contains-all-contents-of-another-map

https://stackoverflow.com/questions/33024356/verify-that-all-key-value-pairs-in-a-map-are-present-in-another-map

答案1

得分: 2

以下是您要翻译的内容:

根据我的了解,没有内置方法可以实现您的要求,containsAll 当一个映射中的所有条目存在于另一个映射中时返回 true

但正如您所说,您可以编写如下的方法:

private static boolean containsAllCounts(Map<String, Integer> firstMap, Map<String, Integer> secondMap) {
    String key;
    int secondMapCount;
    for (Map.Entry<String, Integer> entry : secondMap.entrySet()) {
        key = entry.getKey();
        secondMapCount = entry.getValue();
        if (!firstMap.containsKey(key) || firstMap.get(key) <= secondMapCount) {
            return false;
        }
    }
    return true;
}

当有两个如下的映射时:

Map<String, Integer> firstMap = new HashMap<>();
firstMap.put("A", 2);
firstMap.put("B", 2);
firstMap.put("C", 1);

Map<String, Integer> secondMap = new HashMap<>();
secondMap.put("A", 1);
secondMap.put("B", 1);

输出将会是:

true
false
英文:

As I know, there is no built-in method to do what you desire, containsAll returns true when all the map entries from one map exist in another map.

But, as you said you can write such a method as below;

private static boolean containsAllCounts(Map&lt;String, Integer&gt; firstMap, Map&lt;String, Integer&gt; secondMap) {
    String key;
    int secondMapCount;
    for (Map.Entry&lt;String, Integer&gt; entry : secondMap.entrySet()) {
        key = entry.getKey();
        secondMapCount = entry.getValue();
        if (!firstMap.containsKey(key) || firstMap.get(key) &lt;= secondMapCount) {
            return false;
        }
    }
    return true;
}

When there are two maps as below;

    Map&lt;String, Integer&gt; firstMap = new HashMap&lt;&gt;();
    firstMap.put(&quot;A&quot;, 2);
    firstMap.put(&quot;B&quot;, 2);
    firstMap.put(&quot;C&quot;, 1);

    Map&lt;String, Integer&gt; secondMap = new HashMap&lt;&gt;();
    secondMap.put(&quot;A&quot;, 1);
    secondMap.put(&quot;B&quot;, 1);

    System.out.println(containsAllCounts(firstMap, secondMap));
    System.out.println(containsAllCounts(secondMap, firstMap));

Outputs will be;

true
false

答案2

得分: 2

等于方法将比较哈希值,因此仅用于相等性。我不知道是否有内置函数,但是使用流上的allMatch,您可以轻松地自行进行比较...

使用allMatch(感谢@shmosel):

    tMap.entrySet()
         .stream()
         .allMatch(e -> sMap.containsKey(e.getKey()) && sMap.get(e.getKey()) >= e.getValue())

---- 编辑,最初我有noneMatch的示例,但allMatch更容易理解... 为了完整性,我仍然包含了noneMatch的示例 -----

    tMap.entrySet().stream().noneMatch(entry -> {
        final Integer match = sMap.get(entry.getKey());
        return match == null || entry.getValue() > match;
    });

或者,如果您更喜欢将其作为一行:

    tMap.entrySet()
        .stream()
        .noneMatch(entry -> !sMap.containsKey(entry.getKey()) || entry.getValue() > sMap.get(entry.getKey()));

匹配器将检查tMap中任何未匹配的键或值,是否大于sMap中的值。

英文:

The equal method will compare the hash and therefore only for equality. I am not aware of a build in function, but with an allMatch on the stream you could do a compare easily on your own...

With allMatch (thanks to @shmosel):

    tMap.entrySet()
         .stream()
         .allMatch(e -&gt; sMap.containsKey(e.getKey()) &amp;&amp; sMap.get(e.getKey()) &gt;= e.getValue())

---- edited, originally I had the noneMatch example but allMatch is easier to understand...for completeness, I still include the noneMatch example -----

    tMap.entrySet().stream().noneMatch(entry -&gt; {
        final Integer match = sMap.get(entry.getKey());
        return match == null || entry.getValue() &gt; match;
    });

or if you prefer it as a one-liner

    tMap.entrySet()
        .stream()
        .noneMatch(entry -&gt; !sMap.containsKey(entry.getKey()) || entry.getValue() &gt; sMap.get(entry.getKey()));

The matcher will check for any unmatched key or value from tMap, that is greater than the value in sMap.

huangapple
  • 本文由 发表于 2023年7月3日 11:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定