多级菜单 树结构 排序 前端 后端 java

目录

省流:

正文:

v1.0版

前端传的值:

后端代码:

v2.0版

v3.0版


省流:

前端提交过来整个树即可。

给整个树进行sort。代码如下:

java 复制代码
    public static void sort(List<Node> tree){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            if(o.getChild()!=null){
                sort(o.getChild());
            }
        }
    }

这个只是单纯排序,没有替换parentId。升级版本见正文。

排序完以后,结果如下:

一级节点的sort:1,2,3,4...

二级节点的sort:每个一级节点下的二级节点,都会从1开始排序:1,2,3,4...

bash 复制代码
|-- 一级节点1
    |--二级节点1
    |--二级节点2
    |--二级节点3
|-- 一级节点2
    |--二级节点1
    |--二级节点2
    |--二级节点3
|-- 一级节点3
    |--二级节点1
    |--二级节点2
    |--二级节点3

正文:

v1.0版

前端传的值:

bash 复制代码
{
    "id": "雪花id",
    "parentId": "雪花id",
	"name":"书籍",
	"sort":"1",
    "children": [
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "数学",
            "sort": 1,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "几何学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "代数学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "概率学",
                    "sort": 3,
                }
            ]

        },
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "物理",
            "sort": 2,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "光学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "力学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "量子学",
                    "sort": 3,
                }
            ]
        },
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "化学",
            "sort": 3,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "有机学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "无机学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "应用化学",
                    "sort": 3,
                }
            ]
        }
    ]
}

后端代码:

后端模拟前端传值单元测试:

java 复制代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SortT1 {
    public static void main(String[] args) {
        //一级节点
        Node firstNode1 = new Node();
        Node firstNode2 = new Node();
        Node firstNode3 = new Node();
        List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

        //二级节点
        Node sec1First1 = new Node();
        Node sec2First1 = new Node();
        Node sec3First1 = new Node();
        List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
        firstNode1.setChild(second1);

        Node sec1First2 = new Node();
        Node sec2First2 = new Node();
        Node sec3First2 = new Node();
        List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
        firstNode2.setChild(second2);

        Node sec1First3 = new Node();
        Node sec2First3 = new Node();
        Node sec3First3 = new Node();
        List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
        firstNode3.setChild(second3);

        sort(first);

        System.out.println(first);
    }

    public static void sort(List<Node> tree){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            if(o.getChild()!=null){
                sort(o.getChild());
            }
        }
    }
}
java 复制代码
import java.util.List;

public class Node {
    private Integer sort;
    private List<Node> child;

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public List<Node> getChild() {
        return child;
    }

    public void setChild(List<Node> child) {
        this.child = child;
    }
}

v2.0版

增加parentId

java 复制代码
    public static void main(String[] args) {
        //一级节点
        Node firstNode1 = new Node("0001");
        Node firstNode2 = new Node("0002");
        Node firstNode3 = new Node("0003");
        List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

        //二级节点
        Node sec1First1 = new Node("00010001");
        Node sec2First1 = new Node("00010002");
        Node sec3First1 = new Node("00010003");
        List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
        firstNode1.setChild(second1);

        Node sec1First2 = new Node("00020001");
        Node sec2First2 = new Node("00020002");
        Node sec3First2 = new Node("00020003");
        List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
        firstNode2.setChild(second2);

        Node sec1First3 = new Node("00030001");
        Node sec2First3 = new Node("00030002");
        Node sec3First3 = new Node("00030003");
        List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
        firstNode3.setChild(second3);

        sort(first, "0");

        System.out.println(first);
    }
    public static void sort(List<Node> tree,String parentId){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            o.setParentId(parentId);
            if(o.getChild()!=null){
                sort(o.getChild(),o.getId());
            }
        }
    }
java 复制代码
public class Node {
    public Node(String id){
        this.id = id;
    }
    private Integer sort;
    private List<Node> child;
    private String id;
    private String parentId;
//省略了get set方法没写,自己测的时候记得加上
}

v3.0版

保存到数据库:将所有节点都放到一个list里。

java 复制代码
    public static void main(String[] args) {
        //省略前面的代码
        //...
        List<Node> result = new ArrayList<>();
        sort(first, "0", result);
        //将结果集保存到数据库
        xxxMapper.saveOrUpdate(result);
    }

    public static void sort(List<Node> tree,String parentId,List<Node> result){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            o.setParentId(parentId);
            if(o.getChild()!=null){
                sort(o.getChild(),o.getId(),result);
            }
            o.setChild(null);//递归走出来后就不需要child了
            result.add(o);//将当前节点存到结果集里
        }
    }

注意:为了避免意外发生,生产上记得加上深度,以防万一出现死循环导致栈溢出stackoverflow。

===================分割线===================

文章到此已经结束,以下是紫薯布丁

|-- 一级节点1

|--二级节点1

|--二级节点2

|--二级节点3

|-- 一级节点2

|--二级节点1

|--二级节点2

|--二级节点3

|-- 一级节点3

|--二级节点1

|--二级节点2

|--二级节点3

{

"id": "雪花id",

"parentId": "雪花id",

"name":"书籍",

"sort":"1",

"children": [

{

"id": "雪花id",

"parentId": "雪花id",

"name": "数学",

"sort": 1,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "几何学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "代数学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "概率学",

"sort": 3,

}

]

},

{

"id": "雪花id",

"parentId": "雪花id",

"name": "物理",

"sort": 2,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "光学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "力学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "量子学",

"sort": 3,

}

]

},

{

"id": "雪花id",

"parentId": "雪花id",

"name": "化学",

"sort": 3,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "有机学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "无机学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "应用化学",

"sort": 3,

}

]

}

]

}

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class SortT1 {

public static void main(String[] args) {

//一级节点

Node firstNode1 = new Node();

Node firstNode2 = new Node();

Node firstNode3 = new Node();

List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

//二级节点

Node sec1First1 = new Node();

Node sec2First1 = new Node();

Node sec3First1 = new Node();

List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));

firstNode1.setChild(second1);

Node sec1First2 = new Node();

Node sec2First2 = new Node();

Node sec3First2 = new Node();

List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));

firstNode2.setChild(second2);

Node sec1First3 = new Node();

Node sec2First3 = new Node();

Node sec3First3 = new Node();

List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));

firstNode3.setChild(second3);

sort(first);

System.out.println(first);

}

public static void sort(List<Node> tree){

int i = 0;

for (Node o : tree) {

o.setSort(i++);

if(o.getChild()!=null){

sort(o.getChild());

}

}

}

}

public static void main(String[] args) {

List<Node> result = new ArrayList<>();

sort(first, "0", result);

//将结果集保存到数据库

xxxMapper.saveOrUpdate(result);

}

public static void sort(List<Node> tree,String parentId,List<Node> result){

int i = 0;

for (Node o : tree) {

o.setSort(i++);

o.setParentId(parentId);

if(o.getChild()!=null){

sort(o.getChild(),o.getId(),result);

}

o.setChild(null);//递归走出来后就不需要child了

result.add(o);//将当前节点存到结果集里

}

}

public class Node {

public Node(String id){

this.id = id;

}

private Integer sort;

private List<Node> child;

private String id;

private String parentId;

//省略了get set方法没写,自己测的时候记得加上

}

相关推荐
圈圈编码19 分钟前
Spring Task 定时任务
java·前端·spring
俏布斯32 分钟前
算法日常记录
java·算法·leetcode
276695829237 分钟前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿
爱的叹息38 分钟前
Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
java·redis·spring
程序猿chen1 小时前
《JVM考古现场(十五):熵火燎原——从量子递归到热寂晶壁的代码涅槃》
java·jvm·git·后端·java-ee·区块链·量子计算
松韬1 小时前
Spring + Redisson:从 0 到 1 搭建高可用分布式缓存系统
java·redis·分布式·spring·缓存
绝顶少年2 小时前
Spring Boot 注解:深度解析与应用场景
java·spring boot·后端
心灵宝贝2 小时前
Tomcat 部署 Jenkins.war 详细教程(含常见问题解决)
java·tomcat·jenkins
天上掉下来个程小白2 小时前
Redis-14.在Java中操作Redis-Spring Data Redis使用方式-操作列表类型的数据
java·redis·spring·springboot·苍穹外卖
ゞ 正在缓冲99%…2 小时前
leetcode22.括号生成
java·算法·leetcode·回溯