英文:
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.
答案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<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;
}
When there are two maps as below;
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);
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 -> sMap.containsKey(e.getKey()) && sMap.get(e.getKey()) >= 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 -> {
final Integer match = sMap.get(entry.getKey());
return match == null || entry.getValue() > match;
});
or if you prefer it as a one-liner
tMap.entrySet()
.stream()
.noneMatch(entry -> !sMap.containsKey(entry.getKey()) || entry.getValue() > sMap.get(entry.getKey()));
The matcher will check for any unmatched key or value from tMap, that is greater than the value in sMap.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论