SoFunction
Updated on 2025-04-05

Java recursion and subsetting based on nodes

Java recursion and subsets based on nodes

1. Example

 //Recursive tree    private List<DeptVO> getDeptTree(List<DeptVO> childList, List<DeptVO> parentList) {
        for (DeptVO p : parentList) {
            List<DeptVO> twoLevelMenuTree = ()
                    .filter(s -> ((), ()))
                    .collect(());
            if (() > 0) {
                (twoLevelMenuTree);
                getDeptTree(childList, twoLevelMenuTree);
            }
        }
        return parentList;
    }

2. Recursive parameter explanation

The above childList is a collection of all data, and the parentList is the data of the highest node. Here, the tree structure is formed based on the connection between the superior department id of the child node and the department id of the parent node.

3. Reverse recursion (take all child nodes of child nodes)

public void getChildrenByTree(DeptVO result, List<DeptVO> children, String deptId) {
        if (result == null) {
            return;
        }
        if (().equals(deptId)) {
            if (() != null) {
                (());
                return;
            }
        }
        if (() != null) {
            for (DeptVO child : ()) {
                getChildrenByTree(child, children, deptId);
            }
        }
    }

4. Reverse recursive parameter explanation

result is the data of the top-level parent node (including all child nodes), children is a collection used to receive child node data. When using it, create an empty list pass method, and deptId is the id of the parent node that needs to be found.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.