构建树形结构工具

1.构建一个树形结构的实体类TreeBean

public class TreeBean implements Serializable {

    private static final long serialVersionUID = 1L;


    /*id和pid必须赋值,level看自己有没有这个字段,otherParameter装一些其他参数,比如name,key-value键值对的形式*/
    /*将你从数据库查出来的集合循环,然后对每一行数据进行赋值转换成TreeBean结构,调用TreeUtils工具即可得到树形结构列表*/
    private Object id;//id

    private Object pid;//pid

    private Object level;//级别

    private Map otherParameter;//其他参数,需要展示的参数,比如name之类的


    /*---------------------------------下面这些不需要你赋值-------------------------------------------*/
    private Boolean hasChildren;//是否含有下一级

    private List children;//包含的子节点

    private Integer childrenSize;//子节点的集合大小

}

2.构造树形结构的方法

  /**
     * 树形结构
     * @param assetsSorts 查询出集合
     * @param assetsSortId 顶级节点id
     * @return
     */
    public List buildTree(List assetsSorts,Long assetsSortId) {
        ArrayList treeBeans = new ArrayList<>();
        List treeBeans1 = new ArrayList<>();
        if(!CollectionUtils.isEmpty(assetsSorts)){
            Long topId = 1L;
            TreeBean assetsSortBean  =null;
            for(AssetsSort assetsSort:assetsSorts){
                TreeBean treeBean = new TreeBean();
                treeBean.setId(assetsSort.getAssetsSortId());
                treeBean.setPid(assetsSort.getParentId());
                Map parameter = new HashMap<>();
                parameter.put("assetsSortName", assetsSort.getAssetsSortName());
                parameter.put("assetsSortNames", assetsSort.getAssetsSortNames());
                parameter.put("parentIds", assetsSort.getParentIds());
                parameter.put("createBy", assetsSort.getCreateBy());
                parameter.put("createTime", ObjectUtils.isEmpty(assetsSort.getCreateTime()) ? "":DateUtils.parseDateToStr("yyyy-MM-dd hh:mm:ss",assetsSort.getCreateTime()));
                parameter.put("updateBy", assetsSort.getUpdateBy());
                parameter.put("updateTime", ObjectUtils.isEmpty(assetsSort.getUpdateTime()) ? "":DateUtils.parseDateToStr("yyyy-MM-dd hh:mm:ss",assetsSort.getUpdateTime()));
                parameter.put("deleteFlag", assetsSort.getDeleteFlag());
                treeBean.setOtherParameter(parameter);
                treeBeans.add(treeBean);
                //拿到顶级节点
                if ((!ObjectUtils.isEmpty(assetsSortId)&&NumberUtil.compare(assetsSortId,assetsSort.getAssetsSortId())==0)
                        || NumberUtil.compare(topId,assetsSort.getAssetsSortId())==0){
                    assetsSortBean = treeBean;
                }
            }
            if(!ObjectUtils.isEmpty(assetsSortId)){
                topId = assetsSortId;
            }
            treeBeans1 = TreeUtils.buildByRecursive(treeBeans, topId,assetsSortBean);
        }
        return treeBeans1;
    }

3.快速建树的工具类

import cn.hutool.core.util.NumberUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lephips.utils.bean.TreeBean;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

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

/**
 *  
 *  * @projectName vcsrm-parent
 *  * @title     TreeUtils   
 *  * @package    com.clfps.utils  
 *  * @description    树形结构utils
 *  * @author IT_CREAT     
 *  * @date  2019 2019/7/1 15:39  
 *  * @version V1.0.0 
 *  
 */
public class TreeUtils {

    /**
     * 使用递归方法建树
     * @param
     * @return
     */
    public static List buildByRecursive(List treeBeans, Long topId ,TreeBean topTreeBean) {
        List trees = new ArrayList<>();
        TreeBean treeBeanTop = null;
        for (TreeBean treeBean : treeBeans) {
            if (0 == NumberUtil.compare(treeBean.getPid(),topId)) {
                Long level = 1L;
                if(!ObjectUtils.isEmpty(topTreeBean) && 0 == NumberUtil.compare(topTreeBean.getId(), topId)){
                    level = 2L;
                }
                TreeBean treeBean2 = findChildren(treeBean,treeBeans,level);
                if(CollectionUtils.isEmpty(treeBean2.getChildren())){
                    treeBean2.setHasChildren(false);
                    treeBean2.setLevel(level);
                }else {
                    treeBean2.setHasChildren(true);
                    treeBean2.setLevel(level);
                    treeBean2.setChildrenSize((long) treeBean2.getChildren().size());
                }
                trees.add(treeBean2);
            }else if(!ObjectUtils.isEmpty(topTreeBean) && 0 == NumberUtil.compare(treeBean.getId(), topId)){
                treeBeanTop = treeBean;
            }
        }
        if(!ObjectUtils.isEmpty(treeBeanTop)){
            treeBeanTop.setChildren(trees)
                .setChildrenSize((long) trees.size())
                .setHasChildren(true)
                .setLevel(1L);
            trees = new ArrayList<>();
            trees.add(treeBeanTop);
        }
        return trees;
    }


    /**
     * 递归查找子节点
     * @param
     * @return
     */
    private static TreeBean findChildren(TreeBean treeBean,List treeBeans,Long level) {
        for (TreeBean it : treeBeans) {
            if(0 == NumberUtil.compare(treeBean.getId(), it.getPid())) {
                if (treeBean.getChildren() == null) {
                    treeBean.setChildren(new ArrayList<>());
                }
                TreeBean treeBean2 = findChildren(it,treeBeans,level+1);
                if(!CollectionUtils.isEmpty(treeBean2.getChildren())){
                    treeBean2.setHasChildren(true);
                    treeBean2.setChildrenSize((long) treeBean.getChildren().size());
                    treeBean2.setLevel(level+1);
                }else if(treeBean2.getChildren() == null || treeBean2.getChildren().size() == 0){
                    treeBean2.setHasChildren(false);
                    treeBean2.setLevel(level+1);
                }
                treeBean.getChildren().add(treeBean2);
            }
        }
        return treeBean;
    }


    public static void main(String[] args) {
        TreeBean treeBean = new TreeBean();
        treeBean.setId(1L);
        treeBean.setPid(6000L);
//        treeBean.setLevel(1);
        Map parameter = new HashMap<>();
        parameter.put("name", "sadasda");
        parameter.put("nameEn", "sadasda");
        parameter.put("namePinyin", "sadasda");
        parameter.put("code", "sadasda");
        treeBean.setOtherParameter(parameter);

        TreeBean treeBean2 = new TreeBean();
        treeBean2.setId(2L);
        treeBean2.setPid(6000L);
//        treeBean2.setLevel(1);
        Map parameter2 = new HashMap<>();
        parameter.put("name", "sadasda");
        parameter.put("nameEn", "sadasda");
        parameter.put("namePinyin", "sadasda");
        parameter.put("code", "sadasda");
        treeBean2.setOtherParameter(parameter);

        TreeBean treeBean3 = new TreeBean();
        treeBean3.setId(3L);
        treeBean3.setPid(1L);
//        treeBean3.setLevel(2);
        Map parameter3 = new HashMap<>();
        parameter.put("name", "sadasda");
        parameter.put("nameEn", "sadasda");
        parameter.put("namePinyin", "sadasda");
        parameter.put("code", "sadasda");
        treeBean3.setOtherParameter(parameter);

        TreeBean treeBean4 = new TreeBean();
        treeBean4.setId(4L);
        treeBean4.setPid(1L);
//        treeBean4.setLevel(2);
        Map parameter4 = new HashMap<>();
        parameter.put("name", "sadasda");
        parameter.put("nameEn", "sadasda");
        parameter.put("namePinyin", "sadasda");
        parameter.put("code", "sadasda");
        treeBean4.setOtherParameter(parameter);

        TreeBean treeBean5 = new TreeBean();
        treeBean5.setId(5L);
        treeBean5.setPid(3L);
//        treeBean5.setLevel(3);
        Map parameter5 = new HashMap<>();
        parameter.put("name", "sadasda");
        parameter.put("nameEn", "sadasda");
        parameter.put("namePinyin", "sadasda");
        parameter.put("code", "sadasda");
        treeBean5.setOtherParameter(parameter);

        List treeBeans = new ArrayList<>();
        treeBeans.add(treeBean);
        treeBeans.add(treeBean2);
        treeBeans.add(treeBean3);
        treeBeans.add(treeBean4);
        treeBeans.add(treeBean5);

        System.out.println(buildByRecursive(treeBeans,6000L,null));
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println(objectMapper.writeValueAsString(buildByRecursive(treeBeans,1L,null)));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }
}

 

你可能感兴趣的:(Utils)