2017-03-16 11 views
-1

私はと同じのキーを保持すると予想される2つのHashMapsを持っていますが、その値にはいくつかの違いがあり、おそらくソース/ターゲットにはキーが含まれていません。一致と不一致のHashMapsの比較

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

私はキーのMatchedデータ値、キーのMismatchedデータ値、そして最後にKeys exist in only one mapとの報告書を作成していますよ。

Java 8のcomputeIfPresent()またはcomputeIfAbsent()を使用して、これをどのように達成できますか? sourceマップを繰り返し実行する必要があります。もしkeytargetマップに存在する場合は、検査値が一致するかどうかを確認してください。一致した場合、結果を一致コレクションに出力します。一致しなかったコンテナに出力し、最後にターゲットにキーを出力しません。

+0

、[ツアー](http://stackoverflow.com/tour)を取るの周りを見て、そして一読ください[ヘルプセンター](http://stackoverflow.com/help)、特に[どのように私は良い質問をしますか?](http://stackoverflow.com/help/how-to-ask)私はここで尋ねる?](http://stackoverflow.com/help/on-topic)。 –

答えて

1

私はcomputeIfPresentまたはcomputeIfAbsentが、このために適切であるとは思わない:

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); 
} 
関連する問題