Skip to content

Commit 93a1773

Browse files
authored
Create map比对差异.md
1 parent ef39689 commit 93a1773

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Java/map比对差异.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
```java
2+
import com.google.common.collect.MapDifference;
3+
import com.google.common.collect.Maps;
4+
import com.github.wnameless.json.flattener.JsonFlattener;
5+
import cn.hutool.core.util.XmlUtil;
6+
7+
Map<String, Object> left = XMLUtils.flattenAsMap(leftFile);
8+
Map<String, Object> right = XMLUtils.flattenAsMap(rightFile);
9+
MapDifference<String, Object> difference = Maps.difference(left, right);
10+
11+
Map<String, MapDifference.ValueDifference<Object>> diffMap = difference.entriesDiffering();
12+
Map<String, Object> leftOnlyMap = difference.entriesOnlyOnLeft();
13+
Map<String, Object> rightOnlyMap = difference.entriesOnlyOnRight();
14+
15+
16+
public static Map<String, Object> flattenAsMap(String filePath) {
17+
long s = System.currentTimeMillis();
18+
String xml = XmlUtil.toStr(XmlUtil.readXML(filePath));
19+
String json = JsonUtils.mapToJson(XmlUtil.xmlToMap(xml));
20+
long e = System.currentTimeMillis();
21+
log.info("parseXml2Json {} cost {}", filePath, e - s);
22+
return JsonFlattener.flattenAsMap(json);
23+
}
24+
25+
public static String mapToJson(Map map) {
26+
try {
27+
return new ObjectMapper().writeValueAsString(map);
28+
} catch (Exception e) {
29+
LOGGER.error("mapToJson error:{}", e);
30+
throw new BusinessException("mapToJson exception");
31+
}
32+
}
33+
```

0 commit comments

Comments
 (0)