简体   繁体   English

从 Guava 计算的地图差异构建地图的问题

[英]Issue in constructing map from map differences computed by Guava

I am using Google Guava to compute the difference between two maps, and then with these differences, I would like to construct one more map for further processing as follows:我正在使用谷歌番石榴来计算两张地图之间的差异,然后根据这些差异,我想再构建一张地图以供进一步处理,如下所示:

  Map<String, Map<String, Object>> fieldToDiffMap = new HashMap<>();

  MapDifference<String, LinkedHashMap> diff =
      Maps.difference(oldMap, currMap);

  Map<String, MapDifference.ValueDifference<LinkedHashMap>> entriesDiffering = diff
      .entriesDiffering();

  for (Map.Entry<String, MapDifference.ValueDifference<LinkedHashMap>> entry :
      entriesDiffering.entrySet()) {
    
    Map<String, String> diffMap = new HashMap<>();
    MapDifference.ValueDifference<LinkedHashMap> valueDiff = entry.getValue();
    diffMap.put("Old Value", valueDiff.leftValue().get("value"));  <--THIS LINE GIVING ERROR
    diffMap.put("New Value", valueDiff.rightValue().get("value")); <--THIS LINE GIVING ERROR

    fieldToDiffMap.put(entry.getKey(), diffMap);
  }

Here, I am getting a compiler error:在这里,我收到编译器错误:

   incompatible types: java.lang.Object cannot be converted to java.lang.String

I have tried to handle this by replacing the above two lines as follows:我试图通过如下替换上面的两行来处理这个问题:

diffMap.put("Old Value", valueDiff.leftValue().get("value").toString());
diffMap.put("New Value", valueDiff.rightValue().get("value").toString());

But with these change, the compiler error is gone, code is running successfully, but Strings are getting embedded between additional quotes as follows:但是通过这些更改,编译器错误消失了,代码运行成功,但是字符串被嵌入到附加引号之间,如下所示:

"Test String" is getting converted into ""Test String""

Could anyone please help here?有人可以在这里帮忙吗?

You're using raw types in your map definition.您在地图定义中使用原始类型。 Here's a fixed version which should work for you:这是一个适合您的固定版本:

//given
Map<String, Map<String, Object>> fieldToDiffMap = new HashMap<>();

Map<String, LinkedHashMap<String, Object>> oldMap = ImmutableMap.of(
        "foo", Maps.newLinkedHashMap(ImmutableMap.of("value", "Test String")),
        "bar", Maps.newLinkedHashMap(ImmutableMap.of("value", "Same String"))
);
Map<String, LinkedHashMap<String, Object>> currMap = ImmutableMap.of(
        "foo", Maps.newLinkedHashMap(ImmutableMap.of("value", "\"Test String\"")),
        "bar", Maps.newLinkedHashMap(ImmutableMap.of("value", "Same String"))
);
//when
MapDifference<String, LinkedHashMap<String, Object>> diff = // not raw types MapDifference<String, LinkedHashMap> diff
        Maps.difference(oldMap, currMap);

Map<String, MapDifference.ValueDifference<LinkedHashMap<String, Object>>> entriesDiffering = diff
        .entriesDiffering();

for (Map.Entry<String, MapDifference.ValueDifference<LinkedHashMap<String, Object>>> entry :
        entriesDiffering.entrySet()) {

    Map<String, Object> diffMap = new HashMap<>();
    MapDifference.ValueDifference<LinkedHashMap<String, Object>> valueDiff = entry.getValue();
    diffMap.put("Old Value", valueDiff.leftValue().get("value"));//  <--THIS LINE GIVING ERROR
    diffMap.put("New Value", valueDiff.rightValue().get("value"));// <--THIS LINE GIVING ERROR

    fieldToDiffMap.put(entry.getKey(), diffMap);
}
//then
assertThat(fieldToDiffMap) // {foo={Old Value=Test String, New Value="Test String"}}
        .containsOnly(entry("foo", ImmutableMap.of(
                "Old Value", "Test String",
                "New Value", "\"Test String\""))
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM