String tagsOld= "北京,上海,长春";
String tagsNew= "北京,杭州";
String result="北京,上海,长春,杭州"
public String getSignTags(String tagsOld,String tagsNew) {
Set<String> hashSet = null;
String resultTags = tagsNew + AppConstant.DATA_SEPARATOR + tagsOld;
String result="";
if (resultTags.contains(AppConstant.DATA_SEPARATOR)) {
String[] split = resultTags.split(AppConstant.DATA_SEPARATOR);
hashSet = new LinkedHashSet<>(Arrays.asList(split));
}
if (null != hashSet) {
result = hashSet.stream().collect(Collectors.joining(AppConstant.DATA_SEPARATOR));
}
return result;
}
String oldStr= "{\"国别\":\"法国,中国\",\"行业\":\"纺织,制造\",\"蔬菜\":\"青菜\"}";
String newStr= "{\"国别\":\"中国,美国\",\"行业\":\"纺织\"}";
String resultJson="{\"国别\":\"法国,中国,美国\",\"行业\":\"纺织,制造\",\"蔬菜\":\"青菜\"}";
public String getMergeJson(String oldStr, String newStr) {
Map<String, String> result = new HashMap();
boolean s3flag = JSONUtil.isJson(oldStr);
boolean s4flag = JSONUtil.isJson(newStr);
if (s3flag && s4flag) {
Map<String, String> hashMap3 = JSONUtil.toBean(oldStr, HashMap.class);
Map<String, String> hashMap4 = JSONUtil.toBean(newStr, HashMap.class);
result = hashMap3;
for (String str : hashMap4.keySet()) {
String newValue = hashMap4.get(str);
if (result.containsKey(str)) {
String oldValue = result.get(str);
String mergeStr = getSignTags(oldValue, newValue);
result.put(str, mergeStr);
} else {
result.put(str, newValue);
}
}
}
String resultJson = JSONUtil.toJsonStr(result);
return resultJson;
}