java递归构建菜单树
package testSimple; import java.util.ArrayList;
import java.util.List; public class BuildTree {
public static void main(String[] args) {
List<Tree> list = new ArrayList<Tree>();
list.add(new Tree(1, "一级菜单", 0));
list.add(new Tree(2, "二级菜单1", 1));
list.add(new Tree(3, "二级菜单2", 1));
list.add(new Tree(4, "二级菜单3", 1));
list.add(new Tree(5, "三级菜单11", 2));
list.add(new Tree(6, "三级菜单12", 2));
list.add(new Tree(7, "三级菜单21", 3));
list.add(new Tree(8, "三级菜单22", 3));
list.add(new Tree(9, "三级菜单31", 4));
list.add(new Tree(10, "三级菜单32", 4)); List<Tree> result = buildTree(list, 0);
System.out.println(result);
} public static List<Tree> buildTree(List<Tree> list, int fid) {
List<Tree> resultList = new ArrayList<Tree>();
if (list == null || list.size() == 0 || fid < 0) {
return null;
}
for (Tree tree : list) {
if (tree.getFid() == fid) {
resultList.add(tree);
tree.setChildren(buildTree(list, tree.getId()));
}
}
return resultList;
} } class Tree {
private int id;
private String name;
private int fid;
private List<Tree> children; public Tree(int id, String name, int fid) {
super();
this.id = id;
this.name = name;
this.fid = fid;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getFid() {
return fid;
} public void setFid(int fid) {
this.fid = fid;
} public List<Tree> getChildren() {
return children;
} public void setChildren(List<Tree> children) {
this.children = children;
} }
java递归构建菜单树的更多相关文章
- Mybatis通过colliection属性递归获取菜单树
1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...
- java生成多级菜单树
使用java实现一个多级菜单树结构 先上数据库 ps_pid字段很重要,是父级菜单的id Menu类 Menu类要新增一个字段,用来存放子菜单 /** * 子菜单列表 */ private List& ...
- c#递归读取菜单树
1.查询菜单节点下所有子节点id List<sys_module> menus = new List<sys_module>() { }; public async Task& ...
- java从数据库读取菜单,递归生成菜单树
首先看一下菜单的样子 根据这个样子我们定义菜单类 public class Menu { // 菜单id private String id; // 菜单名称 private String name; ...
- java递归树形菜单显示
今天写权限整好涉及到一些菜单的显示,最先想到的是递归遍历输出,但是呢在网上找了很多有关的资料没有符合自己需求的所以就自己写了一个,如果觉得靠谱 请点点关注!!!赞一个 首先看一些设计的数据库设计 菜单 ...
- Java递归获取部门树 返回jstree数据
@GetMapping("/getDept")@ResponseBodypublic Tree<DeptDO> getDept(String deptId){ Tree ...
- React + Antd Menu组件实现菜单树
准备好两个变量,一个用来保存平级菜单列表,一个用来保存遍历后的菜单树. 推荐后端返回平级菜单树,假如菜单比较多,可以直接结合find方法找到菜单,做搜索功能很省事. const [menuList, ...
- java实现的可以无限级别添加子节点的菜单树
网上大部分菜单树,都是单独用js代码来实现的,这样做的缺点是:用户无法动态的设置菜单项,比如,超级管理员可能需要根据每个用户的权限,赋予他们不同的系统功能,不同的功能对应着不同数量的菜单项. 对于此问 ...
- bootstrap treeview实现菜单树
本博客,介绍通过Bootstrap的treeview插件实现菜单树的功能. treeview链接:http://www.htmleaf.com/Demo/201502141380.html ORM框架 ...
随机推荐
- Decoration2:引入Angularjs显示前台一条数据
SpringMVC内置的RestFul API格式采用的是最复杂最全面的HATEOAS规范,对于简单应用来说,前台解析起来不方便,我们下面主要想办法重新定义一种简单的RestFulAPI. (1)先是 ...
- mac 写NTFS磁盘
最简单的方法就是把 OS X 自带的 mount_ntfs 默认加载方式从只读改成读写, 具体方法如下 # 用 root 身份做如下操作 (高危! 请切记自己在干什么)sudo -s cd /sbin ...
- CentOS 7.0 关闭firewalld防火墙指令 及更换Iptables防火墙
CentOS 7.0 关闭firewalld防火墙指令 及更换Iptables防火墙 时间:2014-10-13 19:03:48 作者:哎丫丫 来源:哎丫丫数码网 查看:11761 评论:2 ...
- SpringBoot 整合 Security5
https://my.oschina.net/yunduansing/blog/2032475 https://blog.csdn.net/SWPU_Lipan/article/details/805 ...
- [转]C++中的三种继承public,protected,private
链接:http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/23/2514964.html
- linux 短信收发
#include <termios.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h> ...
- 无需看到你的脸就能认出你——实现Beyond Frontal Faces: Improving Person Recognition Using Multiple Cues
今年年初Facebook AI Research发布了一篇名为Beyond Frontal Faces: Improving Person Recognition Using Multiple Cue ...
- Linux下配置Hadoop伪分布式环境
1. 准备Linux环境 提示:我用的系统是CentOS 6.4. 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host- ...
- ES-PHP向ES批量添加文档报No alive nodes found in your cluster
ES-PHP向ES批量添加文档报No alive nodes found in your cluster 2016年12月14日 12:31:40 阅读数:2668 参考文章phpcurl 请求Chu ...
- thinkphp 设置 支持模块多组
正常的访问地址是:localhost/项目名/index.php/Admin/Index/index 模块分组之后呢:localhost/项目名/index.php/Admin/System/Inde ...