集合list封装成树,多个子树,任意拼接,任何层级搜索

package com.csw.shuanfa.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TreeBuilder {


    /**
     * parent Id 是空
     *
     * @param nodes
     * @return
     */
    public static List<NodeEntity> buildTree2(List<NodeEntity> nodes) {
        Map<Integer, NodeEntity> nodeMap = new HashMap<>(nodes.size());
        List<NodeEntity> roots = new ArrayList<>();

        //1
        firstChange(nodes);
        //2
        getNodeMap(nodes, nodeMap);


        for (NodeEntity node : nodes) {
            Integer parentId = node.getParentId();
            NodeEntity parent = nodeMap.get(parentId);
            if (parent != null) {
                parent.getChildren().add(node);
            } else {
                roots.add(node);
            }
        }

        return roots;
    }

    private static void firstChange(List<NodeEntity> nodes) {
        for (NodeEntity node : nodes) {
            //初始化子child集合
            if (node.getChildren() == null) {
                node.setChildren(new ArrayList<>());
            }
            if (node.getChildren().size() > 0) {
                firstChange(node.getChildren());
            }
        }
    }

    private static void getNodeMap(List<NodeEntity> nodes, Map<Integer, NodeEntity> nodeMap) {
        for (NodeEntity node : nodes) {
            nodeMap.put(node.getId(), node);
            if (node.getChildren().size() > 0) {
                getNodeMap(node.getChildren(), nodeMap);
            }
        }
    }

    /**
     * 按照每页的id,获取相应的树杈
     *
     * @param nodes
     * @param list
     * @param res
     */
    public static void getNodeRes(List<NodeEntity> nodes, List<Integer> list, List<NodeEntity> res) {
        for (NodeEntity node : nodes) {
            if (list.contains(node.getId())) {
                res.add(node);
            }
            if (node.getChildren().size() > 0) {
                getNodeRes(node.getChildren(), list, res);
            }
        }
    }

    public static void main(String[] args) {
        List<NodeEntity> nodes = new ArrayList<>();
        nodes.add(new NodeEntity(111, null, null));
        nodes.add(new NodeEntity(2, 111, null));
        nodes.add(new NodeEntity(3, 111, null));
        nodes.add(new NodeEntity(5, 2, null));
        nodes.add(new NodeEntity(4, 2, null));
        nodes.add(new NodeEntity(6, 3, null));

        nodes.add(new NodeEntity(7, 222, null));
        nodes.add(new NodeEntity(8, 222, null));
        nodes.add(new NodeEntity(9, 7, null));
        nodes.add(new NodeEntity(222, 0, null));
        nodes.add(new NodeEntity(10, 7, null));
        nodes.add(new NodeEntity(11, 8, null));

        nodes = buildTree2(nodes);
        nodes.add(new NodeEntity(12, 7, null));
        nodes.add(new NodeEntity(13, 8, null));
        nodes.add(new NodeEntity(14, 11, null));
        nodes.add(new NodeEntity(15, 11, null));
        nodes = buildTree2(nodes);
        System.out.println(nodes);
        //模拟获取当前页id集合
        //List<Long> collect = pageResult.getItems().stream().map(NodeEntity::getId).collect(Collectors.toList());
        List<Integer> col = new ArrayList<>();
        col.add(111);
        col.add(5);
        col.add(6);
        col.add(11);
        //假设上面的树里面是根据上面的4条数据递归查询出来的子集合封装成的树。现在一页数据就是4条
        List<NodeEntity> res = new ArrayList<>();
        getNodeRes(nodes, col, res);

        System.out.println(res);
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
class NodeEntity {
    private int id;//
    private Integer parentId;
    private List<NodeEntity> children;
}




``
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容