[英]How to Check if a TreeMap is submap of another TreeMap in Java
New to Java 8, and am unable to figure this one out. Java 8的新增功能,无法弄清楚这一点。 I have two maps of type
TreeMap<Long, Integer>
, one is called patternMap
and the other answerMap
. 我有两个类型为
TreeMap<Long, Integer>
,一个称为patternMap
,另一个称为answerMap
。
patternMap
is hardcoded to look for a particular pattern of key-value pairs, so there are many patternMap
's, and their size is always less than or equal to answerMap
. patternMap
被硬编码为查找键-值对的特定模式,因此有很多patternMap
,并且它们的大小始终小于或等于answerMap
。 I would like to check which patternMap
s are a match in answerMap
, and I mean I need exact match on key and value both, not just one. 我想检查哪个
patternMap
是patternMap
中的匹配answerMap
,我的意思是我需要键和值都精确匹配,而不仅仅是一个。
I'm unable to use the submap
method, because patternMap
may not have a contiguous range of keys. 我无法使用
submap
的方法,因为patternMap
可能没有钥匙的连续范围。 The obvious way is to walk through the patternMap
and check each Map.Entry
object with equals
method. 一种明显的方法是遍历
patternMap
并使用equals
方法检查每个Map.Entry
对象。 However, I have many patternMap
's to check against a particular answerMap
, so I'm curious to know if there is a better way, probably using lambdas/streams. 但是,我有很多
patternMap
可以检查特定的answerMap
,所以我很好奇是否有更好的方法,可能使用了lambdas / streams。 I also cannot modify the answerMap
or the patternMap
, so this has to be a non-mutating function. 我也不能修改
answerMap
或patternMap
,所以这必须是一个非变异函数。 I use TreeMap
's because the ordering is important for the application. 我使用
TreeMap
是因为顺序对于应用程序很重要。
There is no code here because I haven't found a way to do this, but pseudo-code would look like this: 这里没有代码,因为我还没有找到执行此操作的方法,但是伪代码如下所示:
for every patternMap in the Collection {
if answerMap contains patternMap, make a note
}
EDIT: Here are some definitions(this isn't working code, but hopefully makes question clearer: 编辑:这是一些定义(这不是工作代码,但希望可以使问题更清楚:
private static final TreeMap<Long, Integer> patternMap1;
private static final TreeMap<Long, Integer> patternMap2;
private static final ArrayList<TreeMap> listOfPatternMaps;
private TreeMap<Long, Integer> answerMap;
private Set<TreeMap<Long, Integer>> findPatterns(TreeMap answerMap) {
this.answerMap = answerMap;
for(patternMap : listOfPatternMaps) {
if answerMap contains patternMap {
make a note
}
}
return setOfMatchedPatterns;
}
这应该可以解决问题:
answerMap.entrySet().containsAll(patternMap.entrySet())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.