尝试找出此代码的递归版本

时间:2019-04-05 10:58:13

标签: java recursion

我有此代码,效果很好。

String thirdChild = getChild(columnsLength, paramsMap, index + 1);
        String fourthChild = getChild(columnsLength, paramsMap, index + 2);

        if (thirdChild != null) {
                    for(Map<String, Object> child : children) {
                        List<Map<String, Object>> children2 = getChildGroupObjects((List<Map<String, String>>)child.get("items"), aggregatableColumns, thirdChild);
                        if(fourthChild != null) {
                            for(Map<String, Object> child2 : children2) {
                                List<Map<String, Object>> children3 = getChildGroupObjects((List<Map<String, String>>)child2.get("items"), aggregatableColumns, fourthChild);
                                log.info("fourth");
                                child2.put("items", children3);
                                child2.put("subGroups", children3);
                                child2.put("hasSubgroups", true);
                            }
                        }
                        log.info("third");
                        child.put("items", children2);
                        child.put("subGroups", children2);
                        child.put("hasSubgroups", true);
                    }

                }

我尝试在此处制作此代码的递归版本

private List<Map<String, Object>> recursiveGrouping(List<Map<String, Object>> children, List<String> aggregatableColumns,
                                                        String childObject, int columnsLength, Map<String, String[]> paramsMap, int index) {
        String childObj = getChild(columnsLength, paramsMap, index);
        index++;
        if(childObj == null) {
            return new ArrayList<Map<String, Object>>();
        }

        for(Map<String, Object> child : children) {
            List<Map<String, Object>> children2 = getChildGroupObjects((List<Map<String, String>>)child.get("items"), aggregatableColumns, childObj);

            child.put("items", children2);
            child.put("subGroups", children2);
            child.put("hasSubgroups", true);
            List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();
            childList.add(child);



            recursiveGrouping(childList, aggregatableColumns, childObj, columnsLength, paramsMap, index);


        }

        return children;
    }

,但在浏览器上不起作用。请给我一些帮助。如果我的问题有问题,请通知我,我会立即纠正。

该代码应该使用相似的键对哈希图进行分组,但这是另一个函数调用的范围。基本上,在这段代码中,我们只调用该方法,对于循环的每次迭代都深入一步(如果存在)并找出候选分组(hasmap列表)

显示的只有2个级别,我正在使用递归方法将其转到第n个级别。

get child调用将使用HashMap的相同键对子项进行分组。基本上它们是相似的结构,但通常较小的列表大小。

1 个答案:

答案 0 :(得分:0)

显然,这段代码给了我正确的结果

    private List<Map<String, Object>> recursiveGrouping(List<Map<String, Object>> children, List<String> aggregatableColumns,
                                                        String childObject, int columnsLength, Map<String, String[]> paramsMap, int index) {
        String childObj = getChild(columnsLength, paramsMap, index);
        List<Map<String, Object>> childrenToReturn = new ArrayList<Map<String, Object>>();
        index++;
        if(childObj == null) {
            return new ArrayList<Map<String, Object>>();
        }

        for(Map<String, Object> child : children) {
            List<Map<String, Object>> children2 = getChildGroupObjects((List<Map<String, String>>)child.get("items"), aggregatableColumns, childObj);
            recursiveGrouping(children2, aggregatableColumns, childObj, columnsLength, paramsMap, index);
            child.put("items", children2);
            child.put("subGroups", children2);
            if(children2.size() > 0) {
                child.put("hasSubgroups", true);
            }
            childrenToReturn.addAll(children2);
        }

        return childrenToReturn;
    }