工程目录和类结构:
1.Book.java 测试model,包含id和父id还有name。
package com.dlmu.sei;
/**
*
* @author zhangtuo
* @Date 2017/4/14
*/
public class Book {
private String id;
private String parentId;
private String name;
public String getId() {
return id;
}
//省略get和set函数,构造函数等。
}
2.Constant.java 常量定义,父节点的uuid默认都是0字符串
package com.dlmu.sei;
/**
* @author zhangtuo
* @Date 2017/4/14
*/
public class Constant {
public static final String UUID_PARENT = "00000000000000000000000000000000";
}
3.TreeNode.java 树结构model。
package com.dlmu.sei;
import java.util.List;
/**
* iview的tree格式
* @author zhangtuo
* @Date 2017/4/14
*/
public class TreeNode {
private String title;
private boolean extend;//是否扩展
private List childrens;
public String getTitle() {
return title;
}
//省略get和set函数以及toString函数。
}
4.TreeUtil.java 生成树的类,以递归的方式构造。
package com.dlmu.sei;
import java.util.ArrayList;
import java.util.List;
/**
* 生成树的结构
* @author zhangtuo
* @Date 2017/4/14
*/
public class TreeUtil {
/**
* 生出树结构。
* @return
*/
public TreeNode buildTree(List books) {
//最后返回的treeNode
TreeNode treeNode = new TreeNode();
//遍历books
for (Book book : books) {
//拿到该book的父id
String pid = book.getParentId();
//如果pid是超级根节点,可以开始构造。
if (pid.contains(Constant.UUID_PARENT)) {
treeNode.setExtend(true);
treeNode.setTitle(book.getName());
build(book, books, treeNode);
}
}
return treeNode;
}
private void build(Book book, List books, TreeNode treeNode) {
List childrens = getChildrens(book, books);
List childrenNodes = new ArrayList();
//如果该book有叶子,则递归遍历叶子
if (!childrens.isEmpty()) {
for (Book child : childrens) {
String childId = child.getId();
TreeNode childNode = new TreeNode();
childNode.setExtend(true);
childNode.setTitle(child.getName());
childrenNodes.add(childNode);
build(child, books, childNode);
if (childrenNodes.size() != 0) {
treeNode.setChildrens(childrenNodes);
}
}
} else {
//如果没有孩子,是不能扩展,extend为false
treeNode.setExtend(false);
}
}
/**
* 查找某个节点下的叶子节点
* @param book
* @param books
* @return
*/
private List getChildrens(Book book, List books) {
List childrens = new ArrayList();
String id = book.getId();
for (Book child : books) {
//如果遍历中的parentId和book的id相同,则是它的子节点
if (child.getParentId().equals(id)) {
childrens.add(child);
}
}
return childrens;
}
}
5. TreeNodeTest.java 测试函数。
package com.dlmu.sei;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @author zhangtuo
* @Date 2017/4/14
*/
public class TreeNodeTest {
private static String uuid() {
return UUID.randomUUID().toString().replace("-", "");
}
@Test
public void test() {
List books = generateTestData();
TreeUtil treeUtil = new TreeUtil();
TreeNode treeNode = treeUtil.buildTree(books);
System.out.println(JSONArray.fromObject(treeNode));
}
private List generateTestData() {
Book b0 = new Book(uuid(), Constant.UUID_PARENT, "book0");
Book b1 = new Book(uuid(), b0.getId(), "book1");
Book b2 = new Book(uuid(), b0.getId(), "book2");
Book b3 = new Book(uuid(), b0.getId(), "book3");
Book b11 = new Book(uuid(), b1.getId(), "book1-1");
Book b12 = new Book(uuid(), b1.getId(), "book1-2");
Book b111 = new Book(uuid(), b11.getId(), "book1-1-1");
Book b21 = new Book(uuid(), b2.getId(), "book2-1");
Book b31 = new Book(uuid(), b3.getId(), "book3-1");
List books = new ArrayList();
books.add(b0);
books.add(b1);
books.add(b2);
books.add(b3);
books.add(b11);
books.add(b12);
books.add(b111);
books.add(b21);
books.add(b31);
return books;
}
}