Java 如何比较两个哈希映射

Java 如何比较两个哈希映射,java,Java,我在这里填写了两个哈希图: Properties properties = new Properties(); try { properties.load(openFileInput("xmlfilesnames.xml")); } catch (IOException e) { e.printStackTrace(); } for (String key : properties.stringPropertyNames()) { xmlFileMap.put(key,

我在这里填写了两个哈希图:

Properties properties = new Properties();
try {
    properties.load(openFileInput("xmlfilesnames.xml"));
} catch (IOException e) {
    e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
    xmlFileMap.put(key, properties.get(key).toString());
}

try {
    properties.load(openFileInput("comparexml.xml"));
} catch (IOException e) {
    e.printStackTrace();
}
for (String key : properties.stringPropertyNames()) {
    compareMap.put(key, properties.get(key).toString());
}
声明:

public Map<String,String> compareMap = new HashMap<>();
public Map<String, String> xmlFileMap = new HashMap<>();
publicmap compareMap=newhashmap();
publicmap xmlFileMap=新HashMap();
它们看起来像:

如何检查
作业\u id
是否已更改,如果它为空? 有时工作id并不存在。因此,
job\u id
在它们中缺失

有时在
compareMap
中不止一个
job\u id


如何仅比较
作业id
,并在比较时获得
布尔值

似乎希望根据特定模式查找映射键。这可以通过迭代所有键来完成:

private static String PREFIX = "<job_id>";
private static String SUFFIX = "</job_id>";

public static String extractJobId(Map<String, ?> map) {
    for(String key : map.keySet()) {
        if(key.startsWith(PREFIX) && key.endsWith(SUFFIX))
            return key.substring(PREFIX.length(), key.length()-SUFFIX.length());
    }
    // no job_id found
    return null;
}

谢谢你的回答!!令人惊叹的!所以,如果映射不相等,它将返回null,对吗?那么我就知道映射不相等了?@korunos,如果job_id不同,那么
Obejcts.equals
将返回false。如果只是改变了顺序,我会得到什么。假设两个贴图都有两个job_id,并且它们相等。如果比较器映射的作业id改变了顺序怎么办?那么
equals()
呢<代码>比较映射等于(xmlFileMap)
。假设您的键和值都有适当的
equals()
hashCode()
public static Set<String> extractJobIds(Map<String, ?> map) {
    Set<String> result = new HashSet<>();
    for(String key : map.keySet()) {
        if(key.startsWith(PREFIX) && key.endsWith(SUFFIX))
            result.add(key.substring(PREFIX.length(), key.length()-SUFFIX.length()));
    }
    return result;
}
if(Objects.equals(extractJobIds(xmlFileMap), extractJobIds(compareMap))) {
    // ...
}