java 去除jsonarray里面jsonarray的重复和合并数据

原来数据如下:

[{"index_id":"19557485","itemid":"70575","time":"1467619020","value":"1"},

{"index_id":"19557442","itemid":"113795","time":"1467619020","value":"1"},

{"index_id":"19557507","itemid":"114227","time":"1467619020","value":"1"},

{"index_id":"19557534","itemid":"114231","time":"1467619020","value":"1"},

{"index_id":"19557534","itemid":"114233","time":"1467619020","value":"1"},

{"index_id":"19557534","itemid":"114237","time":"1467619020","value":"1"},

{"index_id":"19557534","itemid":"114239","time":"1467619020","value":"1"},

{"index_id":"19557593","itemid":"114241","time":"1467619020","value":"1"},

{"index_id":"20118932","itemid":"115778","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"222222","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"333333","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"444444","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"555555","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"666666","time":"1467619020","value":"1"},

{"index_id":"11111111","itemid":"777777","time":"1467619020","value":"1"},

{"index_id":"19557534","itemid":"1145235","time":"1467619020","value":"1"}]

现在要求合并相同index_id的value值,其实array和list类似,如果遇到list也可用我的方法,新建一个新的arraytemp临时存储json

代码如下:

/**
     * 去重复index_id项合并value值
     * @param args
     */
    public static JSONArray delRepeatIndexid(JSONArray array) {
   
    JSONArray arrayTemp = new JSONArray();
        
        int num = 0;
        for(int i = 0;i < array.size();i++){        
          if(num==0){
          arrayTemp.add(array.get(i));
          }else{
          int numJ = 0;
              for(int j = 0;j < arrayTemp.size(); j++){
              JSONObject newJsonObjectI = (JSONObject)array.get(i);
              JSONObject newJsonObjectJ = (JSONObject)arrayTemp.get(j);
              String  index_idI = newJsonObjectI.get("index_id").toString();
              String  valueI = newJsonObjectI.get("value").toString();
              String  timeI = newJsonObjectI.get("time").toString(); 
          String  itemidI = newJsonObjectI.get("itemid").toString();
             
              String  index_idJ = newJsonObjectJ.get("index_id").toString();
              String  valueJ = newJsonObjectJ.get("value").toString();
             
              if(index_idI.equals(index_idJ)){
              int newValue = Integer.parseInt(valueI) + Integer.parseInt(valueJ);
              arrayTemp.remove(j);
              JSONObject newObject = new JSONObject();
              newObject.put("index_id", index_idI);
              newObject.put("itemid", itemidI);
              newObject.put("time", timeI);
              newObject.put("value", newValue);
              arrayTemp.add(newObject);
              break;
              }
              numJ++;
              }
              if(numJ-1 == arrayTemp.size()-1){
              arrayTemp.add(array.get(i));
              }      
          }
         
         num++;
        }
        return arrayTemp;
    }


输出结果如下:

{"clientip":"10.50.129.11","hostname":"IQSH-D9396","index_gather":[{"hashkey":"","index_id":19557485,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557442,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557507,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557593,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":20118932,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":11111111,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":6},{"hashkey":"","index_id":19557534,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":5}]}


### 如何在 Java去除 JSONArray重复数据Java 中处理 `JSONArray` 并可以通过多种方法实现。以下是几种常见的解决方案: #### 方法一:使用 Set Java 提供了集合类 `Set`,它不允许存储重复的元素。可以利用这一特性来移除数组中的重复项。 ```java import org.json.JSONArray; import java.util.HashSet; public class RemoveDuplicates { public static JSONArray removeDuplicates(JSONArray jsonArray) { HashSet<Object> set = new HashSet<>(); for (int i = 0; i < jsonArray.length(); i++) { set.add(jsonArray.get(i)); // 将每个元素加入到 Set 中 } return new JSONArray(new ArrayList<>(set)); // 转换回 JSONArray } } ``` 此方法的时间复杂度主要取决于底层的数据结构操作,通常为 O(n)[^1]。 --- #### 方法二:基于排序比较相邻元素 如果允许修改原始顺序,则可以先对数组进行排序,随后通过遍历删除连续相同的元素。 ```java import org.json.JSONArray; import java.util.Arrays; public class RemoveDuplicatesSorted { public static JSONArray removeDuplicates(JSONArray jsonArray) throws JSONException { Object[] array = Arrays.copyOf(jsonArray.toArray(), jsonArray.length()); Arrays.sort(array); // 对数组进行排序 JSONArray result = new JSONArray(); Object prev = null; for (Object obj : array) { if (!obj.equals(prev)) { // 如果当前对象不同于前一个对象 result.put(obj); prev = obj; } } return result; } } ``` 这种方法适用于已知输入范围较小的情况,时间复杂度大约为 O(n log n),其中排序占主导地位[^4]。 --- #### 方法三:哈希表辅助法 当无法直接依赖于内置容器时,可手动维护一个布尔型标志位记录哪些值已经存在过。 ```java import org.json.JSONArray; import java.util.HashMap; public class RemoveDuplicatesHashMap { public static JSONArray removeDuplicates(JSONArray jsonArray) { HashMap<Object, Boolean> map = new HashMap<>(); JSONArray result = new JSONArray(); for (int i = 0; i < jsonArray.length(); ++i) { Object value = jsonArray.get(i); if (!map.containsKey(value)) { // 只有未见过才添加至结果集 map.put(value, true); result.put(value); } } return result; } } ``` 上述代码片段展示了如何借助额外空间降低算法运行开销,其平均性能接近线性级别即 O(n)[^3]。 --- ### 性能对比分析 | **方案** | **优点** | **缺点** | |------------------------|---------------------------------------------------------------------------------------------|-------------------------------------| | 使用 Set | 实现简单直观;自动过滤掉冗余条目 | 需要额外内存支持 | | 排序加邻近检测 | 不需引入外部库文件 | 修改原有排列次序 | | Hash 表索引机制 | 支持保持初始布局不变 | 存储成本较高 | 综上所述,在实际开发过程中应根据具体需求权衡选用最合适的策略。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值