Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数据量大的情况 #7

Open
BradyXs opened this issue Nov 15, 2017 · 1 comment
Open

数据量大的情况 #7

BradyXs opened this issue Nov 15, 2017 · 1 comment

Comments

@BradyXs
Copy link

BradyXs commented Nov 15, 2017

如果数据量上千条的话,计算父子关系很耗时,有什么优化方案吗?

@chendabai
Copy link

替换框架内TreeHelper中convetData2Node方法为下方法即可

/**
     * 设置Node间,父子关系;让每两个节点都比较一次,即可设置其中的关系
     */
    private static List<Node> convetData2Node(List<Node> nodes) {
        List<Node> res = convert(nodes);
        return res;
    }

    public static List<Node> convert(List<Node> sourceNodeList) {
        List<Node> destNodeList = new ArrayList<Node>();
        //第一步,找出第一级的节点
        //1.1 统计所有节点的id
        List<String> allIds = new ArrayList<String>();
        Map<String, List<Node>> pIdMap = new HashMap<>();
        for (Node tempNode : sourceNodeList) {
            allIds.add((String) tempNode.getId());
            //构造一个存储Pid得map.递归中直接使用键值查找
            String pid = (String) tempNode.getpId();
            if (pIdMap.containsKey(pid)) {
                pIdMap.get(pid).add(tempNode);
            } else {
                List<Node> valueList = new ArrayList<>();
                valueList.add(tempNode);
                pIdMap.put(pid, valueList);
            }

        }

        //所有父节点找不到对应的都是一级id
        for (Node sourceNode : sourceNodeList) {
            if (!allIds.contains(sourceNode.getpId())) {
                //从每个一级节点,递归查找children
                Node destNode = new Node();
                destNode.setId(sourceNode.getId());
                destNode.setName(sourceNode.getName());
                destNode.setpId(sourceNode.getpId());
                destNode.setLevel(1);
                destNode.bean = sourceNode.bean;
                destNode.iconExpand = sourceNode.iconExpand;
                destNode.iconNoExpand = sourceNode.iconNoExpand;
                List<Node> myChilds = getChilderen(destNode, pIdMap);
                destNode.setChildren(myChilds.isEmpty() ? new ArrayList<>() : myChilds);
                destNodeList.add(destNode);
            }
        }
        return destNodeList;
    }


    //    递归获取子节点
    private static List<Node> getChilderen(Node parentNode, Map<String, List<Node>> pIdMap) {
        List<Node> childrenList = new ArrayList<Node>();
        //使用map来进行查找优化效率
        if (!pIdMap.containsKey(parentNode.getId())) {
            return childrenList;
        } else {
            List<Node> sourceNode = pIdMap.get(parentNode.getId());
            for (Node node : sourceNode) {
                Node children = new Node();
                children.setId(node.getId());
                children.setName(node.getName());
                children.setpId(node.getpId());
                children.setLevel(node.getLevel() + 1);
                children.bean = node.bean;
                children.iconExpand = node.iconExpand;
                children.iconNoExpand = node.iconNoExpand;
                children.setParent(parentNode);
                List<Node> myChilds = getChilderen(children, pIdMap);
                children.setChildren(myChilds.isEmpty() ? new ArrayList<>() : myChilds);
                childrenList.add(children);
            }

            return childrenList;
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants