Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

sorting - How to simplify grouping of data to n times by using Map recursion in java?

I want to return a group of groups to n times in the following ways;

[
    {
      "field": "TestContacts_CompanyName",                                       
      "value": "MC 1",                         
      "hasSubgroups": true,                 
      "items": [            
        {
          "field": "TestContacts_ID",          
          "value": "25",          
          "hasSubgroups": false,        
          "items": [           
            {           
              "TestContacts_ID": "25",       
              "TestContacts_CompanyName": "MC 1"       
            }      
          ]
        },                  
        {
          "field": "TestContacts_ID",                   
          "value": "26",                       
          "hasSubgroups": false,                   
          "items": [                   
            {
              "TestContacts_ID": "26",                         
              "TestContacts_CompanyName": "MC 1"                     
            }          
          ]
        },
        {
          "field": "TestContacts_ID",                  
          "value": "27",                     
          "hasSubgroups": false,                       
          "items": [      
            {
              "TestContacts_ID": "27",                        
              "TestContacts_CompanyName": "MC 1"                     
            }
          ]
        }
      ]
    },
    {
      "field": "TestContacts_CompanyName",                        
      "value": "MC 2",                        
      "hasSubgroups": true,                      
      "items": [
        {          "field": "TestContacts_ID",                        
          "value": "28",                        
          "hasSubgroups": false,                    
          "items": [
            {
              "TestContacts_ID": "28",                            
              "TestContacts_CompanyName": "MC 2"                      
            }
          ]
        }
      ]
    }
  ]    

I don't want to use collectors.groupingby

public static JSONArray groupedArray(ArrayList<String> groupField,JSONArray itemsArray) throws Exception {
        JSONArray itemArrayTemp = new JSONArray();
        int i=0;
         
         for (Map.Entry<String,ArrayList<Object>> entry : SLQUtil.groupData(groupField.get(i),itemsArray).entrySet()) {
                JSONObject itemTemp = new JSONObject();
             
                 itemTemp.put("field",groupField.get(i) );
                 itemTemp.put("value",entry.getKey() );
                 
             if((groupField.size()-i ==1)){
                 itemTemp.put("hasSubgroups",false );//Single level grouping
                 itemTemp.put("items",entry.getValue() );
             }else if((groupField.size()-i > 1)){
                 itemTemp.put("hasSubgroups",true );//2nd level grouping
                 JSONArray jsArray2 = new JSONArray(entry.getValue());//converting array list to json array
                 JSONArray itemArrayTemp2 = new JSONArray();
                 for (Map.Entry<String,ArrayList<Object>> entry2 : SLQUtil.groupData(groupField.get(i+1),jsArray2).entrySet()) {
                        JSONObject itemTemp2 = new JSONObject();
                     itemTemp2.put("field",groupField.get(i+1));
                     itemTemp2.put("value",entry2.getKey() );
                         itemTemp2.put("hasSubgroups",false );
                         itemTemp2.put("items",entry2.getValue() );
                 
                     itemArrayTemp2.put(itemTemp2 );//2nd level of grouped data
                 }
                 itemTemp.put("items",itemArrayTemp2 );
             }
             itemArrayTemp.put(itemTemp);//1st level of grouped data
         }

       return itemArrayTemp;
    }


    public static Map<String, ArrayList<Object>> groupData(String field,JSONArray itemsArray) throws  Exception {

        Map<String, ArrayList<Object>> itemMap = new LinkedHashMap<>();
        for (int i=0;i<itemsArray.length();i++){
            JSONObject itemTemp = itemsArray.getJSONObject(i);
            
            if(!itemMap.containsKey(itemTemp.getString(field))){
              itemMap.put(itemTemp.getString(field), new ArrayList<>());
            }
            itemMap.get(itemTemp.getString(field)).add(itemTemp);
        }

       return itemMap;
    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
public static Map<String, Object> groupDataRecursive(String currentKey, Map<String, Object> map, Map<String, Object> out) throws SQLException, Exception {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getValue() instanceof Map) {
            groupDataRecursive(currentKey, (Map<String, Object>) entry.getValue(), out);
        } else {
            Map tempMap = SLQUtil.groupData(currentKey,(JSONArray)entry.getValue());
            map.put( entry.getKey(),tempMap);
            //out.put(currentKey + "." + entry.getKey(), entry.getValue());
        }
    }
    return map;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...