废话不多说了,具体实现原来直接上博主的以往博客
代码地址: https://gitee.com/baichen9187/treeUtil
代码一
package com.items.Tree;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class TreeNodeUtil<E> {
private List<E> boList;
private Function<E, Boolean> RootNodeFunc;
private IsParent<E> NodeFunc;
public TreeNodeUtil(final List<E> boList) {
this.boList = boList;
}
public void setRootNodeFunc(Function<E, Boolean> rootNodeFunc) {
this.RootNodeFunc = rootNodeFunc;
}
public void setNodeFunc(IsParent<E> nodeFunc) {
this.NodeFunc = nodeFunc;
}
/**
* 获取根节点的列表
* @param function
* @return
*/
private List<E> getRootNode(final Function<E, Boolean> function) {
final List<E> rootMenuLists = new ArrayList<>();
for (E menuNode : boList) {
if (function.apply(menuNode)){
rootMenuLists.add(menuNode);
}
}
return rootMenuLists;
}
/**
* 建立树形结构
* @return List<E>
*/
public List<E> buildTree(final Customer<E> consumer) {
final List<E> treeMenus = new ArrayList<>();
for (E e : getRootNode(RootNodeFunc)) {
e = buildChildrenTree(e,consumer,NodeFunc);
treeMenus.add(e);
}
return treeMenus;
}
/**
* 递归,建立子树形结构
* @param pNode
* @return E
*/
private E buildChildrenTree(final E pNode, final Customer<E> consumer, final IsParent<E> function) {
final List<E> childrenMenus = new ArrayList<E>();
for (E e : boList) {
if (function.access(pNode,e)) {
childrenMenus.add(buildChildrenTree(e,consumer,function));
}
}
consumer.access(pNode, childrenMenus);
return pNode;
}
}
@Resource
private UnitsMapper unitsMapper;
public void test() {
List<UnitsBO> list = unitsMapper.selectList(
new LambdaQueryWrapper<>());
TreeNodeUtil<UnitsBO> treeNodeUtil = new TreeNodeUtil<>(list);
treeNodeUtil.setRootNodeFunc((bo) -> bo.getParentId() == null);
treeNodeUtil.setNodeFunc((bo, parent) -> bo.getId().equals(parent.getParentId()));
List<UnitsBO> tree = treeNodeUtil.buildTree(UnitsBO::setChildren);
System.out.println(tree);
}
代码二
package com.items.TreeUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class ITreeUtilImpl<E,R> implements ITreeUtil<E,R> {
/**
* 获取父节点的方法
*/
private Function<E, R> parentFunc;
/**
* 获取节点的方法
*/
private Function<E, R> codeFunc;
/**
* 设置子节点的方法
*/
private Customer<E> consumer;
/**
* 获取子组织列表的方法
*/
private Function<E, List<E>> listFunc;
/**
* 源数据
*/
private List<E> boList;
public ITreeUtilImpl(){}
public ITreeUtilImpl(List<E> list) {
this.boList = list;
}
public List<E> getBoList() {
return boList;
}
public void setBoList(List<E> boList) {
this.boList = boList;
}
public void setListFunc(Function<E, List<E>> listFunc) {
this.listFunc = listFunc;
}
public void setParentFunc(Function<E, R> parentFunc) {
this.parentFunc = parentFunc;
}
public void setCodeFunc(Function<E, R> codeFunc) {
this.codeFunc = codeFunc;
}
public void setConsumer(Customer<E> consumer) {
this.consumer = consumer;
}
@Override
public List<E> getTreeList(final List<E> eList) {
final List<E> list = new ArrayList<>();
for (E e : eList) {
list.add(this.getTree(e, boList));
}
return list;
}
@Override
public E getTree(E e, List<E> boList) {
//所有节点
final Map<R, E> map = new HashMap<>();
final Map<R, List<E>> eMap = new HashMap<>();
for (E bo : boList) {
R ParentCode = parentFunc.apply(bo);
R code = codeFunc.apply(bo);
if (!eMap.containsKey(ParentCode)) {
eMap.put(ParentCode, new ArrayList<>());
}
map.put(code, bo);
eMap.get(ParentCode).add(bo);
}
// e = getTreeGen(map, e);
this.getSubordinate(eMap, e, listFunc);
return e;
}
private void getSubordinate(Map<R, List<E>> boMap, E e, Function<E, List<E>> listFunc) {
R code = codeFunc.apply(e);
if (boMap.containsKey(code)) { // 从map中查询子组织列表
consumer.access(e, boMap.get(code)); //设置bo的子组织
for (E e1 : listFunc.apply(e)) { // 遍历子组织列表
this.getSubordinate(boMap, e1, listFunc);
}
}
}
@Override
public E getTreeGen(final Map<R, E> map, E bo) {
while (true) {
R ParentCode = parentFunc.apply(bo);
if (ParentCode == null || ParentCode == "") {
break;
}
final E e = map.get(ParentCode);
if (e != null) {
bo = e;
} else {
break;
}
}
return bo;
}
}
demo
package com.items;
import com.items.TreeUtil.ITreeUtilImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TreeDemo {
static List<Test> testList = new ArrayList<Test>(){{
add(new Test("1","一",null));
add(new Test("2","一",null));
add(new Test("3","一",null));
add(new Test("1.1","一","1"));
add(new Test("1.2","一","1"));
add(new Test("1.1.1","一","1.1"));
add(new Test("2.1","一","2"));
}};
static class Test{
String id;
String name;
String Pid;
List<Test> children;
public Test(String id, String name, String pid) {
this.id = id;
this.name = name;
Pid = pid;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPid() {
return Pid;
}
public void setPid(String pid) {
Pid = pid;
}
public List<Test> getChildren() {
return children;
}
public void setChildren(List<Test> children) {
this.children = children;
}
@Override
public String toString() {
return "Test{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", Pid='" + Pid + '\'' +
", children=" + children +
'}';
}
}
public static void main(String[] args) {
ITreeUtilImpl<Test, String> treeUtil = new ITreeUtilImpl<>();
treeUtil.setBoList(testList);
treeUtil.setCodeFunc(Test::getId);
treeUtil.setParentFunc(Test::getPid);
treeUtil.setListFunc(Test::getChildren);
treeUtil.setConsumer(Test::setChildren);
List<Test> trees = treeUtil.getTreeList(testList.stream().filter(item-> item.getPid() == null).collect(Collectors.toList())
);
System.out.println(trees);
}
}