文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java递归实现树形结构的方式有哪些

2023-07-04 11:13

关注

这篇文章主要介绍“Java递归实现树形结构的方式有哪些”,在日常操作中,相信很多人在Java递归实现树形结构的方式有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java递归实现树形结构的方式有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

0、引言

在开发的过程中,很多业务场景需要一个树形结构的结果集进行前端展示,也可以理解为是一个无限父子结构,常见的有报表指标结构、菜单结构等。Java中递归实现树形结构的两种常见方式如下:

1、数据准备

Java实体类NodePO对应数据库表

package com.wbs.pojo;import lombok.Data;import lombok.NoArgsConstructor;import java.util.List;@Data@NoArgsConstructorpublic class NodePO {        private String id;        private String name;        private String parentId;        private String orderNo;        private List<NodePO> children;        public NodePO(String id,String name,String parentId,String orderNo){        this.id = id;        this.name = name;        this.parentId = parentId;        this.orderNo = orderNo;    }}

自己造一些数据模拟从数据库中查询出来的数据:

static final List<NodePO> nodePOs = Arrays.asList(            new NodePO("1","一级节点1",null,"_0001"),            new NodePO("2","二级节点1.1","1","_0002"),            new NodePO("3","二级节点1.2","1","_0003"),            new NodePO("4","一级节点2",null,"_0004"),            new NodePO("5","二级节点2.1","4","_0005"),            new NodePO("6","二级节点2.2","4","_0006"),            new NodePO("7","三级节点2.2.1","6","_0007"),            new NodePO("8","一级节点3",null,"_0008"),            new NodePO("9","二级节点3.1","8","_0009"),            new NodePO("10","三级节点3.1.1","9","_0010"),            new NodePO("11","四级节点3.1.1.1","10","_0011"),            new NodePO("12","五级节点3.1.1.1.1","11","_0012")    );

2、类型转化

从开发的过程中发现直接操作实体类集合,专门指定某一个实体类封装的方法是不具有普适性的,所以将实体类集合统一转化为Map集合,操作方便,具有一定的普适性:

List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(jsonObject);

BeanMapUtils自己简单封装一个工具类(不惧普适性勿喷):

package com.wbs.util;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.google.common.collect.Lists;import com.google.common.collect.Maps;import lombok.SneakyThrows;import org.springframework.cglib.beans.BeanMap;import java.util.*;import java.util.function.Function;import java.util.stream.Collectors;public class BeanMapUtils {        public static <T> Map<String, Object> beanToMap(T t) {        Map<String, Object> map = new HashMap<>();        if (t != null) {            if (t instanceof JSONObject){                return (JSONObject)t;            }            BeanMap beanMap = BeanMap.create(t);            for (Object key : beanMap.keySet()) {                map.put(key.toString(), beanMap.get(key));            }        }        return map;    }        public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws Exception {        T bean = clazz.newInstance();        if (bean instanceof JSONObject){            JSONObject jsonObject = (JSONObject)bean;            Set<Map.Entry<String, Object>> entries = map.entrySet();            for (Map.Entry<String, Object> entry : entries) {                jsonObject.put(entry.getKey(),entry.getValue());            }            return (T)jsonObject;        }        BeanMap beanMap = BeanMap.create(bean);        beanMap.putAll(map);        return bean;    }        public static <T> List<Map<String, Object>> listBeanToListMap(List<T> objList) {        return objList.stream().map(new Function<T, Map<String, Object>>() {            @Override            public Map<String, Object> apply(T t) {                Map<String,Object> map = Maps.newHashMap();                if (t instanceof JSONObject){                    return (JSONObject)t;                }                BeanMap beanMap = BeanMap.create(t);                for (Object key : beanMap.keySet()) {                    map.put(key.toString(), beanMap.get(key));                }                return map;            }        }).collect(Collectors.toList());    }        public static <T> List<T> listMapToListBean(List<Map<String,Object>> mapList,Class<T> clazz) {        return mapList.stream().map(new Function<Map<String, Object>,T>() {            @SneakyThrows            @Override            public T apply(Map<String, Object> map) {                T t = clazz.newInstance();                if (t instanceof JSONObject){                    return (T)map;                }                BeanMap beanMap = BeanMap.create(t);                beanMap.putAll(map);                return t;            }        }).collect(Collectors.toList());    }}

其中org.springframework.cglib.beans.BeanMap;org.springframework:spring-core依赖下的工具包,spring-core核心依赖只要导入spring-boot-starter依赖即可

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter</artifactId>    <version>2.2.0.RELEASE</version></dependency>

Java递归实现树形结构的方式有哪些

3、递归实现方法

3.1、Java7及以下纯Java递归实现

既然是Java7及以下实现方式,那排序也用最原始的冒泡排序:

    public static List<Map<String, Object>> sortJava7Map(List<Map<String, Object>> list){        if(CollectionUtils.isEmpty(list)){            return Lists.newArrayList();        }        boolean flag;        int size = list.size();        for (int i = 0; i < size - 1; i++) {            flag = false;            for (int j = 1; j < size - i; j++) {                Map<String, Object> frontMap = list.get(j - 1);                Map<String, Object> afterMap = list.get(j);                if (String.valueOf(frontMap.get("orderNo")).compareTo(String.valueOf(afterMap.get("orderNo"))) > 0){                    list.set(j - 1,afterMap);                    list.set(j,frontMap);                    flag = true;                }            }            //如果没有发生位置互换,则退出循环            if (!flag){                break;            }        }        return list;    }

给定一个节点,获取它的所有子节点:

    public static List<Map<String, Object>> getJava7Children(Map<String,Object> parentNode,List<Map<String, Object>> allList){        //存放当前节点的直系子节点        List<Map<String, Object>> curNodeChildrenList = Lists.newArrayList();        //存放直系子节点以外的节点        List<Map<String, Object>> otherNodeList = Lists.newArrayList();        Object pId = parentNode.get("id");        for (Map<String, Object> map : allList) {            Object curPId = map.get("parentId");            if (ObjectUtils.isNotEmpty(curPId) && Objects.equals(pId,curPId)){                curNodeChildrenList.add(map);            }else {                otherNodeList.add(map);            }        }        if (curNodeChildrenList.isEmpty()){            return curNodeChildrenList;        }        //每一层级都进行排序        curNodeChildrenList = sortJava7Map(curNodeChildrenList);        //迭代直系子节点再获取子节点        for (Map<String, Object> map : curNodeChildrenList) {            map.put("children",getJava7Children(map,otherNodeList));        }        return curNodeChildrenList;    }

给出一个结果集,构建树形结果集:

    public static List<Map<String, Object>> getJava7ResultTree(List<Map<String, Object>> allList){        //存放所有的一级节点        List<Map<String, Object>> oneLevelNodeList = Lists.newArrayList();        for (Map<String, Object> map : allList) {            if (ObjectUtils.isEmpty(map.get("parentId"))){                map.put("children",getJava7Children(map,allList));                oneLevelNodeList.add(map);            }        }        return sortJava8Map(oneLevelNodeList);    }

获取树形结构:

//转化为Map集合List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(nodePOs);//获取树形结构List<Map<String, Object>> java7ResultTree = getJava7ResultTree(mapList);//打印输出System.out.println(JSON.toJSONString(java7ResultTree));

打印结果:

[{"orderNo":"_0001","children":[{"orderNo":"_0002","children":[],"name":"二级节点1.1","id":"2","parentId":"1"},{"orderNo":"_0003","children":[],"name":"二级节点1.2","id":"3","parentId":"1"}],"name":"一级节点1","id":"1"},{"orderNo":"_0004","children":[{"orderNo":"_0005","children":[],"name":"二级节点2.1","id":"5","parentId":"4"},{"orderNo":"_0006","children":[{"orderNo":"_0007","children":[],"name":"三级节点2.2.1","id":"7","parentId":"6"}],"name":"二级节点2.2","id":"6","parentId":"4"}],"name":"一级节点2","id":"4"},{"orderNo":"_0008","children":[{"orderNo":"_0009","children":[{"orderNo":"_0010","children":[{"orderNo":"_0011","children":[{"orderNo":"_0012","children":[],"name":"五级节点3.1.1.1.1","id":"12","parentId":"11"}],"name":"四级节点3.1.1.1","id":"11","parentId":"10"}],"name":"三级节点3.1.1","id":"10","parentId":"9"}],"name":"二级节点3.1","id":"9","parentId":"8"}],"name":"一级节点3","id":"8"}]

树形结构搞定!

3.2、Java8及以上借助lamda表达式实现

Java7的方式虽然实现了树形结构,但是有一定的缺点,比如:代码量比较大,逻辑相对较复杂,那Java8是如何简化,如下所示:

既然Java8有lamda表达式,那代码我们能省就省,先看排序,一行代码搞定:

    public static List<Map<String, Object>> sortJava8Map(List<Map<String, Object>> list){        if(CollectionUtils.isEmpty(list)){            return Lists.newArrayList();        }        //关键之处,一行代码搞定        list.sort(Comparator.comparing(m -> String.valueOf(m.get("orderNo"))));        return list;    }

给定一个节点,获取它的所有子节点:

释义:
filter: 过滤,相当于for循环,再if条件判断。
peek: 给定一个节点,往它的children塞子节点。

    public static List<Map<String, Object>> getJava8Children(Map<String,Object> parentNode, List<Map<String, Object>> allList){        return allList.stream()                .filter(curNode -> ObjectUtils.isNotEmpty(curNode.get("parentId")) && Objects.equals(curNode.get("parentId"),parentNode.get("id")))                .peek(m -> m.put("children", getJava8Children(m,allList))).collect(Collectors.toList());    }

给出一个结果集,构建树形结果集:

    public static List<Map<String, Object>> getJava8ResultTree(List<Map<String, Object>> mapList){        if (CollectionUtils.isEmpty(mapList)){            return Lists.newArrayList();        }        //filter过滤出所有的一级节点        return mapList.stream().filter(m -> Objects.equals(m.get("parentId"), null) || Objects.equals(m.get("parentId"), ""))                .peek(m -> m.put("children", sortJava8Map(getJava8Children(m, mapList)))).collect(Collectors.toList());    }

获取树形结构:

//转化为Map集合List<Map<String, Object>> mapList = BeanMapUtils.listBeanToListMap(nodePOs);//获取树形结构List<Map<String, Object>> java8ResultTree = getJava8ResultTree(mapList);//打印输出System.out.println(JSON.toJSONString(java8ResultTree));

打印结果:

[{"orderNo":"_0001","children":[{"orderNo":"_0002","children":[],"name":"二级节点1.1","id":"2","parentId":"1"},{"orderNo":"_0003","children":[],"name":"二级节点1.2","id":"3","parentId":"1"}],"name":"一级节点1","id":"1"},{"orderNo":"_0004","children":[{"orderNo":"_0005","children":[],"name":"二级节点2.1","id":"5","parentId":"4"},{"orderNo":"_0006","children":[{"orderNo":"_0007","children":[],"name":"三级节点2.2.1","id":"7","parentId":"6"}],"name":"二级节点2.2","id":"6","parentId":"4"}],"name":"一级节点2","id":"4"},{"orderNo":"_0008","children":[{"orderNo":"_0009","children":[{"orderNo":"_0010","children":[{"orderNo":"_0011","children":[{"orderNo":"_0012","children":[],"name":"五级节点3.1.1.1.1","id":"12","parentId":"11"}],"name":"四级节点3.1.1.1","id":"11","parentId":"10"}],"name":"三级节点3.1.1","id":"10","parentId":"9"}],"name":"二级节点3.1","id":"9","parentId":"8"}],"name":"一级节点3","id":"8"}]

树形结构搞定!两种实现方式对比一下,你就说Java8的方式哇塞不哇塞!!!

到此,关于“Java递归实现树形结构的方式有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯