java递归获取某个父节点下面的所有子节点
点击查看代码
static List<Menu> childMenu=new ArrayList<Menu>();
/**
* 获取某个父节点下面的所有子节点
* @param menuList
* @param pid
* @return
*/
public static List<Menu> treeMenuList( List<Menu> menuList, int pid){
for(Menu mu: menuList){
//遍历出父id等于参数的id,add进子节点集合
if(Integer.valueOf(mu.getPid())==pid){
//递归遍历下一级
treeMenuList(menuList,Integer.valueOf(mu.getId()));
childMenu.add(mu);
}
}
return childMenu;
}
https://www.cnblogs.com/northli/p/15247846.html
原文连接:https://blog.csdn.net/FIQ_527/article/details/122689773
package top.atpisher.com.tacocloud.test;
public class TreeNode{
int id;
int pid;
String name;
public TreeNode() {
super();
}
public TreeNode(int id, int pid) {
super();
this.id = id;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
package top.atpisher.com.tacocloud.test;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class TreeUtil {
//子节点
static List<TreeNode> childMenu = new ArrayList<TreeNode>();
public List<TreeNode> treeMenuList1(List<TreeNode> menuList, int pid) {
for (TreeNode mu : menuList) {
//遍历出父id等于参数的id,add进子节点集合
if (Integer.valueOf(mu.getPid()) == pid) {
//递归遍历下一级
treeMenuList1(menuList, Integer.valueOf(mu.getId()));
childMenu.add(mu);
}
}
return childMenu;
}
// 过滤掉未选中的记录
public List<TreeNode> fiterCatalogs(List<TreeNode> allData, Integer selectid) {
List<TreeNode> chooseData = new ArrayList<>();
Queue<Integer> query = new LinkedList<Integer>();
query.add(selectid);
for (TreeNode item : allData) {
if (selectid.equals(item.getId())) {
chooseData.add(item);
break;
}
}
while (!query.isEmpty()) {
Integer selectidTemp = query.poll();
for (TreeNode item : allData) {
if (selectidTemp.equals(item.getPid())) {
query.add(item.getId());
chooseData.add(item);
}
}
}
return chooseData;
}
/**
* 0---1----4
* +---5
* +---2---6
* +---7
* +---3---8---15
* +---9---10---11
* +---12
* +---13
* +---14
* @param args
*/
public static void main(String[] args) {
TreeUtil tree=new TreeUtil();
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
List<TreeNode> childNode = new ArrayList<TreeNode>();
treeNodes.add(new TreeNode(1,0));
treeNodes.add(new TreeNode(2,0));
treeNodes.add(new TreeNode(3,0));
treeNodes.add(new TreeNode(4,1));
treeNodes.add(new TreeNode(5,1));
treeNodes.add(new TreeNode(6,2));
treeNodes.add(new TreeNode(7,2));
treeNodes.add(new TreeNode(8,3));
treeNodes.add(new TreeNode(9,3));
treeNodes.add(new TreeNode(15,8));
treeNodes.add(new TreeNode(10,9));
treeNodes.add(new TreeNode(11,10));
treeNodes.add(new TreeNode(12,10));
treeNodes.add(new TreeNode(13,10));
treeNodes.add(new TreeNode(14,10));
int rootId = 0;
System.out.println("查找 rootId = " + rootId + "的子节点");
// 开始时间
long stime = System.nanoTime();
tree.treeMenuList1(treeNodes,rootId);
// for(TreeNode node :) {
// System.out.println(" childNode id = " + node.getId());
// }
// 结束时间
long etime = System.nanoTime();
// 计算执行时间
System.out.printf("执行时长:%d 纳秒.", (etime - stime));
System.out.println();
// 开始时间
long stime1 = System.nanoTime();
tree.fiterCatalogs(treeNodes, rootId);
// for(TreeNode node : ) {
// System.out.println(" childNode id = " + node.getId());
// }
// 结束时间
long etime1 = System.nanoTime();
// 计算执行时间
System.out.printf("执行时长:%d 纳秒.", (etime1 - stime1));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
运行结果
查找 rootId = 0的子节点
执行时长:376200 纳秒.
执行时长:116700 纳秒.
Process finished with exit code 0
- 1
- 2
- 3
- 4
求助大佬,能不能有效率高一点的方法