文章出自:http://www.cnblogs.com/dingyingsi/p/3699870.html
maven pom.xml
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency>
TreeBuilder.java
package com.yusj; import java.util.ArrayList; import java.util.List; import org.apache.commons.collections.CollectionUtils; public class TreeBuilder { @SuppressWarnings("unchecked") private List<Node> buildListToTree(List<Node> dirs) { List<Node> roots = findRoots(dirs); List<Node> notRoots = (List<Node>) CollectionUtils .subtract(dirs, roots); for (Node root : roots) { root.setChildren(findChildren(root, notRoots)); } return roots; } public List<Node> findRoots(List<Node> allNodes) { List<Node> results = new ArrayList<Node>(); for (Node node : allNodes) { boolean isRoot = true; for (Node comparedOne : allNodes) { if (node.getParentId() == comparedOne.getId()) { isRoot = false; break; } } if (isRoot) { node.setLevel(0); results.add(node); node.setRootId(node.getId()); } } return results; } @SuppressWarnings("unchecked") private List<Node> findChildren(Node root, List<Node> allNodes) { List<Node> children = new ArrayList<Node>(); for (Node comparedOne : allNodes) { if (comparedOne.getParentId() == root.getId()) { comparedOne.setParent(root); comparedOne.setLevel(root.getLevel() + 1); children.add(comparedOne); } } List<Node> notChildren = (List<Node>) CollectionUtils.subtract(allNodes, children); for (Node child : children) { List<Node> tmpChildren = findChildren(child, notChildren); if (tmpChildren == null || tmpChildren.size() < 1) { child.setLeaf(true); } else { child.setLeaf(false); } child.setChildren(tmpChildren); } return children; } private List<Node> getLeafChildren(List<Node> resultList, List<Node> children){ for(Node node : children){ if(node.isLeaf()){ resultList.add(node); }else{ getLeafChildren(resultList, node.getChildren()); } } return resultList; } public static void main(String[] args) { TreeBuilder tb = new TreeBuilder(); List<Node> allNodes = new ArrayList<Node>(); allNodes.add(new Node(1, 0, "节点1")); allNodes.add(new Node(2, 0, "节点2")); allNodes.add(new Node(3, 0, "节点3")); allNodes.add(new Node(4, 1, "节点4")); allNodes.add(new Node(5, 1, "节点5")); allNodes.add(new Node(6, 1, "节点6")); allNodes.add(new Node(7, 4, "节点7")); allNodes.add(new Node(8, 4, "节点8")); allNodes.add(new Node(9, 5, "节点9")); allNodes.add(new Node(10, 5, "节点10")); allNodes.add(new Node(11, 7, "节点11")); allNodes.add(new Node(12, 7, "节点12")); // 显示所有节点 List<Node> roots = tb.buildListToTree(allNodes); for (Node n : roots) { System.out.println(n); } System.out.println("------------------"); // 查找所有子节点 List<Node> children = tb.findChildren(new Node(1, 0, "节点1"), allNodes); for (Node n : children) { System.out.println(n); } // 查找所有叶子节点 System.out.println("------------------"); List<Node> resultList = tb.getLeafChildren(new ArrayList<Node>(), children); for (Node n : resultList) { System.out.println(n); } } }
Node.java
package com.yusj; import java.util.List; public class Node implements java.io.Serializable { private static final long serialVersionUID = -2721191232926604726L; private int id; private int parentId; private Node parent; private List<Node> children; private String name; private int level; private int sort; private int rootId; private String type; private boolean isLeaf; private String description; public Node() { super(); } public Node(int id, int parentId, String name) { super(); this.id = id; this.parentId = parentId; this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public String getType() { return type; } public List<Node> getChildren() { return children; } public void setChildren(List<Node> children) { this.children = children; } public void setType(String type) { this.type = type; } public boolean isLeaf() { return isLeaf; } public void setLeaf(boolean isLeaf) { this.isLeaf = isLeaf; } public int getSort() { return sort; } public void setSort(int sort) { this.sort = sort; } public int getRootId() { return rootId; } public void setRootId(int rootId) { this.rootId = rootId; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + parentId; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Node other = (Node) obj; if (id != other.id) return false; if (parentId != other.parentId) return false; return true; } @Override public String toString() { return "Node {id=" + id + ", parentId=" + parentId + ", children=" + children + ", name=" + name + ", level =" + level + ", isLeaf =" + isLeaf + "}"; } }
shell
---------所有节点--------- Node {id=1, parentId=0, children=[Node {id=4, parentId=1, children=[Node {id=7, parentId=4, children=[Node {id=11, parentId=7, children=[], name=节点11, level =3, isLeaf =true}, Node {id=12, parentId=7, children=[], name=节点12, level =3, isLeaf =true}], name=节点7, level =2, isLeaf =false}, Node {id=8, parentId=4, children=[], name=节点8, level =2, isLeaf =true}], name=节点4, level =1, isLeaf =false}, Node {id=5, parentId=1, children=[Node {id=9, parentId=5, children=[], name=节点9, level =2, isLeaf =true}, Node {id=10, parentId=5, children=[], name=节点10, level =2, isLeaf =true}], name=节点5, level =1, isLeaf =false}, Node {id=6, parentId=1, children=[], name=节点6, level =1, isLeaf =true}], name=节点1, level =0, isLeaf =false} Node {id=2, parentId=0, children=[], name=节点2, level =0, isLeaf =false} Node {id=3, parentId=0, children=[], name=节点3, level =0, isLeaf =false} ---------<节点1>子节点--------- Node {id=4, parentId=1, children=[Node {id=7, parentId=4, children=[Node {id=11, parentId=7, children=[], name=节点11, level =3, isLeaf =true}, Node {id=12, parentId=7, children=[], name=节点12, level =3, isLeaf =true}], name=节点7, level =2, isLeaf =false}, Node {id=8, parentId=4, children=[], name=节点8, level =2, isLeaf =true}], name=节点4, level =1, isLeaf =false} Node {id=5, parentId=1, children=[Node {id=9, parentId=5, children=[], name=节点9, level =2, isLeaf =true}, Node {id=10, parentId=5, children=[], name=节点10, level =2, isLeaf =true}], name=节点5, level =1, isLeaf =false} Node {id=6, parentId=1, children=[], name=节点6, level =1, isLeaf =true} --------<节点1>叶子节点---------- Node {id=11, parentId=7, children=[], name=节点11, level =3, isLeaf =true} Node {id=12, parentId=7, children=[], name=节点12, level =3, isLeaf =true} Node {id=8, parentId=4, children=[], name=节点8, level =2, isLeaf =true} Node {id=9, parentId=5, children=[], name=节点9, level =2, isLeaf =true} Node {id=10, parentId=5, children=[], name=节点10, level =2, isLeaf =true} Node {id=6, parentId=1, children=[], name=节点6, level =1, isLeaf =true}
相关推荐
利用jxl包结合java反射机制和注释,直接把list生成对应的Excel文件,即只需传入list、对应生成的对象、标题就可以生成excel文件write(String title, List list,Class c),实现一个简单生成excel工具类
4、优点:使用代码生成驼峰形式的字段,可以减少出错概率,生成的实体类字段符合Java命名规范,易于阅读和理解。 5、使用示例:将"TITLE \n" +"COMPANY "可以转换为 /** * TITLE */ @Column(name = ...
tableList:配置成数据库表名,中间以逗号隔开 2、配置start.bat 配置config.path=本地的mybatisplus.json路径,使用正斜杠/ 3、可按照资源中提供的建表sql导入到数据库,然后双加start.bat,即可在上方配置的...
List<Student> list = new ArrayList(); Random rnd = new Random(); for (int i = 0; i ; i++) { Student student = new Student(); student.setCj(rnd.nextDouble()*100); student.setName("name"+i); list....
java freemark list嵌套导出word
所以你要生成ftl扩展名的文件,应该将文件名从 list.ftl => list.ftl.ftl 七. 模板自动include所有父目录的:macro.include文件,可以存放公共的macro 示例: 如你的模板为 com/project/UserDao.java, 将自动...
主要为大家详细介绍了Java实现简单树结构的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
自己写的java生成excel报表jar包,使用html中的table标签样式设计报表头部信息xml文件,接受list数据集生成excel报表。对结构不是太复杂的excel报表十分好用,有详细使用说明。不断完善中,欢迎提供意见和建议!(注...
GeoJSON是一种对各种地理数据结构进行编码的格式,基于Javascript对象表示法(JavaScript Object Notation, 简称JSON)的地理空间信息数据交换格式...该工具可以实现通过java代码将任意的实体类数据集合生成GeoJSON字符串
可加载树形菜单的帮助文档,能够方便的生成各式各样的dhtmlxTree
java生成word.不用再找了,这就是你想要的,包含jar包,下载即运行
主要介绍了Java 生成随机字符串数组的实例详解的相关资料,主要是利用Collections.sort()方法对泛型为String的List 进行排序,需要的朋友可以参考下
* 文件名:RssUtils.java 网站RSS生成 * 版本信息:V1.0 * 日期:2013-06-18 * Copyright BDVCD Corporation 2013 * 版权所有 http://www.bdvcd.com */ public class RssUtils { public static String ...
NULL 博文链接:https://shihuan830619.iteye.com/blog/805948
在java.beans包中,有两个好东西,XMLEncoder和XMLDecoder。从XML存取对象真是太费力气啦。做了小工具类,以后可以用用了。本文介绍了这两个可以把JAVA对象序列化保存为XML文件的工具类。
从Java源生成PlantUML源文件的工具。 参见 @startuml img/default-all.png ' --- package java2plant.model{ class ClassFilter { - classes:ClassList - filters:ArrayList + ClassFilter(classes:ClassList): + ...
通过java的动态编译加载技术,根据java文件动态生成Mysql表创建语句,并且在mysql数据库中创建表
工具简介 1.工具通过自定义的代码模板,生成和数据库相关的代码。比如后台Bean,Dao,Service,前台List页面,编辑页面,新建页面,详情页面等。...2.可以生成各种语言,比如JAVA,Flex,Jsp,Html等。
使用java poi动态编辑生成word文件,生成报表信息。支持插入循环list数据和单个数据得工具类,和测试模板,直接导入就能使用
CallBuilder 是一个 Java 代码生成器,它可以制作生成器类更加容易。当你的构造函数或者方法有多个参数时,生成器就更加好用。它们甚至在你有两个或者更多同类型参数时,也不会混淆。示例代码:public class ...