首页 > 编程语言 >Java树形结构

Java树形结构

时间:2024-04-26 15:57:27浏览次数:18  
标签:comment Java List tree 树形 UCommonTree treeList null 结构

表结构

create table common_tree
(
    id            bigint       not null comment '主键' primary key,
    p_id          bigint       null comment '父节点id',
    tree_code     varchar(100) null comment '树形区分',
    tree_describe varchar(255) null comment '树形描述',
    node_name     varchar(200) null comment '节点名称',
    node_value    varchar(100) null comment '节点值',
    node_index    int          null comment '排序'
)
    comment '树形结构表';

代码

import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author : mc
 * @date : 2023-03-15 10:20
 */
@Service
public class CommonTreeBiz {

    @Resource
    private UCommonTreeMapper treeMapper;

    public List<UCommonTree> buildTree(CommonTreeCodeEnum treeCodeEnum) {
        List<UCommonTree> treeList = new ArrayList<>();

        List<UCommonTree> trees = selectTree(treeCodeEnum);
        for (UCommonTree tree : this.getRootNode(trees)) {
            this.buildChilTree(tree, trees);
            treeList.add(tree);
        }
        return treeList;
    }

    private List<UCommonTree> selectTree(CommonTreeCodeEnum treeCodeEnum) {
        Example example = new Example(UCommonTree.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("treeCode", treeCodeEnum.getCode());
        criteria.andEqualTo("disableFlag", "1");
        example.setOrderByClause("node_index asc");
        return treeMapper.selectByExample(example);
    }

    private List<UCommonTree> getRootNode(List<UCommonTree> trees) {
        List<UCommonTree> rootList = new ArrayList<>();
        for (UCommonTree tree : trees) {
            if (tree.getPId().equals("0")) {
                rootList.add(tree);
            }
        }
        return rootList;
    }

    private UCommonTree buildChilTree(UCommonTree tree, List<UCommonTree> list) {
        List<UCommonTree> treeList = new ArrayList<>();
        for (UCommonTree t : list) {
            if (t.getPId().equals(tree.getId())) {
                treeList.add(this.buildChilTree(t, list));
            }
        }
        tree.setChildren(treeList);
        return tree;
    }
}

标签:comment,Java,List,tree,树形,UCommonTree,treeList,null,结构
From: https://www.cnblogs.com/MC-Bonnie/p/18160234

相关文章

  • java反汇编命令手册
    1.栈和局部变量操作1.1将常量压入栈的指令指令功能描述aconst_null将null对象引用压入栈iconst_m1将将int类型常量-1压入栈iconst_0将int类型常量0压入栈iconst_1将int类型常量1压入栈iconst_2将int类型常量2压入栈iconst_3将int类型常量3压入......
  • Java四种实现单例模式
    饿汉式/***1.饿汉式:线程安全,耗费资源*场景:*资源共享:当需要在多个模块中共享同一个实例时*全局访问点:作为全局唯一的访问点,例如日志记录器、配置管理器等。*线程安全要求高:饿汉式单例模式在类加载时就创建实例,因此不存在线程安全问题,适合多线程环境下使用。*避......
  • 【网络通信】初探网络层次结构(OSI七层网络模型)
    ​        随着信息技术的飞速发展,网络通信已经成为现代社会不可或缺的一部分。网络通信的实现离不开网络协议栈的支持,而网络协议栈则是由多个层次组成的。这些层次各自承担着不同的任务,共同构成了网络通信的基石。本文将对网络通信中的各类层进行详细介绍,包括它们的定......
  • 从 Java 8 转换到 Java 11
    截至目前(2024年),十年前发布的Java8依然是Java中应用最广泛的版本,占比37%,其次是Java11。而目前的JDK最新版本为22,最新的LTS版本为JDK21。从Java8迁移到Java11可能意味着很大的工作量。潜在问题包括:删除的API、弃用的包、内部API的使用、对类加载程序的更......
  • 24数媒Java上机2
    对于一个包含N个非负整数的数组A[0..n-1],如果有0<=i<j<n,且A[i]>A[j],则称(A[i],A[j])为数组A中的一个逆序对。现给定一组数,请你算出这组数中共有多少组逆序对。输入格式:共两行,第一行是一个整数N(N≤1000),表示这组数的个数。第二行有N个整数,用空格分隔,为这组数。测试......
  • Java面试题:SimpleDateFormat是线程安全的吗?使用时应该注意什么?
    在日常开发中,我们经常会用到时间,我们有很多办法在Java代码中获取时间。但是不同的方法获取到的时间的格式都不尽相同,这时候就需要一种格式化工具,把时间显示成我们需要的格式。最常用的方法就是使用SimpleDateFormat类。这是一个看上去功能比较简单的类,但是,一旦使用不当也有可能导......
  • JavaScript---FileReader、Blob、File对象
    ArrayBuffer对象,Blob对象ArrayBuffer对象ArrayBuffer对象表示一段二进制数据,用来模拟内存里面的数据。通过这个对象,JavaScript可以读写二进制数据。这个对象可以看作内存数据的表达。这个对象是ES6才写入标准的,普通的网页编程用不到它,为了教程体系的完整,下面只提供一......
  • 支付宝JavaScript跳转领取红包
    代码如下<!DOCTYPEhtml><html><head> <title>赚钱红包</title> <metacharset="utf-8"> <metaname="wechat-enable-text-zoom-em"content="true"> <metahttp-equiv="Content-Type"......
  • 不只有 Spring,这四款 Java 基础开发框架同样值得关注! 审核中
    Java开发不只有Spring,今天给大家推荐几个同样优秀的Java基础开发框架,为日常项目开发提供更多的选择。答应我,请不要再叫我Spring小子了,​好吗?项目概览:Guice:轻量级依赖注入框架Javalin:轻量级Java和KotlinWeb框架Quarkus:云原生时代高性能Java框架Vert.x:构建响应......
  • 数据结构算法题
    数据结构算法题通过键盘输入一个包括'('和')'的字符串string,判断字符串是否有效。要求设计算法实现检查字符串是否有效,有效的字符串需满足以下条件:A.左括号必须用相同类型的右括号闭合。B.左括号必须以正确的顺序闭合。C.每个右括号都有一个对应的相同类型的左括号。思......