树形结构递归_Java,List,对象列表,排序,平铺结构转树形结构

本文分享了如何在Java中处理数据库中树形结构存储的数据,通过递归方法将列表数据转化为树形结构。面对大量数据时,直接递归查询可能效率低下,文中提供了一个代码案例来解决这个问题。
摘要由CSDN通过智能技术生成

场景说明:

在项目实际过程中,经常遇见二维表(数据库表)为树形结构存储数据,但是递归查询需要好多次,特别是数据量大时,变得非常不合适~

以下是项目中,将列表转换组装为树形结构的代码共享~

平铺的数据:

40c0c9115938776b92046891fa7a4711.png

参考代码案例:

import com.sxmfyy.health.model.system.menu.HealthSysMenu;import com.sxmfyy.health.model.system.menu.dto.HealthSysMenuDTO;import org.springframework.beans.BeanUtils;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class TileTreeConverter {    /**     * 算法====>将平铺结构转换为树形结构     * 实现方案====>采用递归方式     *     * @param sysMenuList     * @return     */    public static List convertTree(List sysMenuList) {        if (sysMenuList == null || sysMenuList.size() <= 0) {            return new ArrayList();        }        int topSign = -1;        List healthSysMenuDTOList = findChildren(sysMenuList, topSign);        buildTree(sysMenuList, healthSysMenuDTOList);        return healthSysMenuDTOList;    }    /**     * @param sysMenuList     * @param parentId     * @return     */    private static List findChildren(List sysMenuList, int parentId) {        // 根据父节点找到子节点,以集合的形式返回        List healthSysMenuDTOList = new ArrayList();        for (HealthSysMenu healthSysMenu : sysMenuList) {            if (parentId == healthSysMenu.getParentId()) {                HealthSysMenuDTO healthSysMenuDTO = new HealthSysMenuDTO();                healthSysMenuDTO.setId(healthSysMenu.getMenuId());                healthSysMenuDTO.setParentId(healthSysMenu.getParentId());                BeanUtils.copyProperties(healthSysMenu, healthSysMenuDTO);                healthSysMenuDTOList.add(healthSysMenuDTO);            }        }        sorted(healthSysMenuDTOList);        return healthSysMenuDTOList;    }    /**     * @param sysMenuList     * @param sysMenuDTOList     * @return     */    private static void buildTree(List sysMenuList, List sysMenuDTOList) {        // 如果存在子节点,再次递归子节点        if (sysMenuDTOList != null && sysMenuDTOList.size() > 0) {            for (HealthSysMenuDTO healthSysMenuDTO : sysMenuDTOList) {                List subSysMenuDTOList = findChildren(sysMenuList, healthSysMenuDTO.getParentId());                if (subSysMenuDTOList != null && subSysMenuDTOList.size() > 0) {                    healthSysMenuDTO.setChildren(subSysMenuDTOList);                }            }        }    }    /**     * 排序     * @param healthSysMenuDTOList     */    private static void sorted(List healthSysMenuDTOList){        Collections.sort(healthSysMenuDTOList,new Comparator() {            @Override            public int compare(HealthSysMenuDTO t1, HealthSysMenuDTO t2) {                if(t1.getSort() > t2.getSort()){                    return 1;                }else if(t1.getSort() < t2.getSort()){                    return -1;                }                return 0;            }        });    }    /**     * main方法==>测试验证     *     * @param args     */    public static void main(String[] args) {        List sysMenuList = new ArrayList();        // 构建顶层节点,其父节点为"-1"        HealthSysMenu healthSysMenu1001 = new HealthSysMenu();        healthSysMenu1001.setMenuId(1001L);        healthSysMenu1001.setParentId(-1);        healthSysMenu1001.setName("1001");        healthSysMenu1001.setSort(1);        sysMenuList.add(healthSysMenu1001);        HealthSysMenu healthSysMenu1002 = new HealthSysMenu();        healthSysMenu1002.setMenuId(1002L);        healthSysMenu1002.setParentId(-1);        healthSysMenu1002.setName("1002");        healthSysMenu1002.setSort(3);        sysMenuList.add(healthSysMenu1002);        HealthSysMenu healthSysMenu1003 = new HealthSysMenu();        healthSysMenu1003.setMenuId(1003L);        healthSysMenu1003.setParentId(-1);        healthSysMenu1003.setName("1003");        healthSysMenu1003.setSort(2);        sysMenuList.add(healthSysMenu1003);        // 构建节点的子节点        HealthSysMenu healthSysMenu100101 = new HealthSysMenu();        healthSysMenu100101.setMenuId(100101L);        healthSysMenu100101.setParentId(1001);        healthSysMenu100101.setName("100101");        healthSysMenu100101.setSort(1);        sysMenuList.add(healthSysMenu100101);        HealthSysMenu healthSysMenu100102 = new HealthSysMenu();        healthSysMenu100102.setMenuId(100102L);        healthSysMenu100102.setParentId(1001);        healthSysMenu100102.setName("100102");        healthSysMenu100102.setSort(3);        sysMenuList.add(healthSysMenu100102);        HealthSysMenu healthSysMenu100103 = new HealthSysMenu();        healthSysMenu100103.setMenuId(100103L);        healthSysMenu100103.setParentId(1001);        healthSysMenu100103.setName("100102");        healthSysMenu100103.setSort(2);        sysMenuList.add(healthSysMenu100103);        // 转换成树形结构对象        List convertTree = convertTree(sysMenuList);        System.out.println(convertTree);    }}

代码案例的执行效果:

2ed04466c69613dfa6281d1773b1449d.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值