合并两个字符串并去重

    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;
   }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值