Java 比较匹配和不匹配的哈希映射

Java 比较匹配和不匹配的哈希映射,java,collections,hashmap,comparison,Java,Collections,Hashmap,Comparison,我有两个HashMaps,它们应该包含相同的键,但它们的值可能会有一些差异,而且源/目标可能不包含该键 Map<String, Double> source = repository.getSourceData(); Map<String, Double> target = repository.getTargetData(); Map source=repository.getSourceData(); Map target=repository.getT

我有两个
HashMaps
,它们应该包含相同的键,但它们的值可能会有一些差异,而且源/目标可能不包含该键

   Map<String, Double> source = repository.getSourceData();
   Map<String, Double> target = repository.getTargetData();
Map source=repository.getSourceData();
Map target=repository.getTargetData();
我希望生成一个报告,其中包含键的
匹配的
数据值,
不匹配的
键的数据值,最后,
键只存在于一个映射中


使用Java 8的
computeIfPresent()
computeifapsent()
,我如何实现这一点?我需要遍历
映射,检查
目标
映射中是否存在
,如果存在,检查值是否匹配。匹配后,将结果输出到匹配的集合。当不匹配时,输出到不匹配的容器,最后输出目标中不存在密钥

我认为ComputeiPresent或ComputeFabSent不适合:

Map<String, Double> source = repository.getSourceData();
Map<String, Double> target = repository.getTargetData();

Map <String, Double> matched = new HashMap<>();
Map <String, List<Double>> mismatched = new HashMap<>();
Map <String, String> unmatched = new HashMap<>();
for (String keySource : source.keySet()) {
    Double dblSource = source.get(keySource);
    if (target.containsKey(keySource)) { // keys match
        Double dblTarget = target.get(keySource);
        if (dblSource.equals(dblTarget)) { // values match
            matched.put(keySource, dblSource);
        } else { // values don't match
            mismatched.put(keySource, new ArrayList<Double>(Arrays.toList(dblSource, dblTarget)));
        }
    } else { // key not in target
        unmatched.put(keySource, "Source");
    }
}
for (String  keyTarget : target.keySet()) { // we only need to look for unmatched
    Double dblTarget = target.get(keyTarget);
    if (!source.containsKey(keyTarget)) {
        unmatched.put(keyTarget, "Target");
    }
}

// print out matched
System.out.println("Matched");
System.out.println("=======");
System.out.println("Key\tValue");
System.out.println("=======");
for (String key : matched.keySet()) {
    System.out.println(key + "\t" + matched.get(key).toString());
}

// print out mismatched
System.out.println();
System.out.println("Mismatched");
System.out.println("=======");
System.out.println("Key\tSource\tTarget");
System.out.println("=======");
for (String key : mismatched.keySet()) {
    List<Double> values = mismatched.get(key);
    System.out.println(key + "\t" + values.get(0) + "\t" + values.get(1));
}

// print out unmatched
System.out.println();
System.out.println("Unmatched");
System.out.println("=======");
System.out.println("Key\tWhich\tValue");
System.out.println("=======");
for (String key : unmatched.keySet()) {
    String which = unmatched.get(key);
    Double value = null;
    if ("Source".equals(which)) {
        value = source.get(key);
    } else {
        value = target.get(key);
    }
    System.out.println(key + "\t" + which + "\t" + value);
}
Map source=repository.getSourceData();
Map target=repository.getTargetData();
Map matched=新的HashMap();
映射不匹配=新建HashMap();
Map unmatched=新HashMap();
for(字符串keySource:source.keySet()){
Double dblSource=source.get(keySource);
如果(target.containsKey(keySource)){//keys匹配
Double dblTarget=target.get(keySource);
如果(dblSource.equals(dblTarget)){//值匹配
matched.put(keySource,dblSource);
}否则{//值不匹配
不匹配的.put(keySource,new ArrayList(Arrays.toList(dblSource,dblTarget));
}
}else{//键不在目标中
不匹配。put(键源,“源”);
}
}
对于(String keyTarget:target.keySet()){//我们只需要查找不匹配的
Double dblTarget=target.get(keyTarget);
如果(!source.containsKey(keyTarget)){
不匹配。put(键目标,“目标”);
}
}
//打印输出匹配
系统输出打印项次(“匹配”);
System.out.println(“==========”);
System.out.println(“Key\tValue”);
System.out.println(“==========”);
for(字符串键:matched.keySet()){
System.out.println(key+“\t”+matched.get(key.toString());
}
//打印输出不匹配
System.out.println();
System.out.println(“不匹配”);
System.out.println(“==========”);
System.out.println(“Key\tSource\tTarget”);
System.out.println(“==========”);
for(字符串键:不匹配的.keySet()){
列表值=不匹配。获取(键);
System.out.println(key+“\t”+values.get(0)+“\t”+values.get(1));
}
//打印输出不匹配
System.out.println();
System.out.println(“不匹配”);
System.out.println(“==========”);
System.out.println(“Key\twich\tValue”);
System.out.println(“==========”);
for(字符串键:不匹配的.keySet()){
字符串,该字符串=不匹配的.get(键);
双值=空;
如果(“源”。等于(哪个)){
value=source.get(键);
}否则{
value=target.get(键);
}
System.out.println(键+“\t”+哪个+“\t”+值);
}
请拿起,四处看看,并通读,尤其是和。