首页 > 解决方案 > 试图找出这段代码的递归版本

问题描述

我有这个工作正常的代码。

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;
    }

但它不适用于浏览器。我可以请一些帮助。如果我的问题有问题,请告知我,我会立即更正。

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

显示的只是 2 个级别,我正在使用递归方法让它进入第 n 级。

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

标签: javarecursion

解决方案


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

    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;
    }


推荐阅读