Java list如何根据id获取子节点
作者:学无终 时间:2023-09-04 09:42:37
工作中因业务需求,将数据库中的树状结构的数据根据父节点获取所有的子节点
实现思路
1.获取整个数据的list集合数据
2.将数据分组,java8 list有groupby分组,java8之前的自己遍历整理
3.分组后递归获取子节点,有子节点的添加,没有的设置子节点并删除分组的数据,知道分组数据删完
Tree.java
@Data
public class Tree {
private Integer id;
private Integer pId;
private String key;
private String value;
private List<Tree> childList;
}
TreeUtils.java
public class TreeUtils {
static List<Tree> trees ;
static {
String jsonStr = "[" +
"{\"id\":100,\"pId\":1,\"key\":\"root\", \"value\": \"root\"}," +
"{\"id\":1000,\"pId\":100,\"key\":\"node1\", \"value\": \"node1\"}," +
"{\"id\":2000,\"pId\":100,\"key\":\"node2\",\"value\": \"node2\"}," +
"{\"id\":3000,\"pId\":100,\"key\":\"node3\",\"value\": \"node3\"}," +
"{\"id\":1100,\"pId\":1000,\"key\":\"node11\",\"value\": \"node11\"}," +
"{\"id\":1200,\"pId\":1000,\"key\":\"node12\",\"value\": \"node12\"}," +
"{\"id\":1110,\"pId\":1100,\"key\":\"node111\",\"value\": \"node111\"}," +
"{\"id\":1120,\"pId\":1100,\"key\":\"node112\",\"value\": \"node112\"}," +
"{\"id\":2100,\"pId\":2000,\"key\":\"node21\",\"value\": \"node21\"}," +
"{\"id\":2200,\"pId\":2000,\"key\":\"node22\",\"value\": \"node22\"}," +
"{\"id\":2110,\"pId\":2100,\"key\":\"node211\",\"value\": \"node21\"}" +
"]";
trees = JSONObject.parseArray(jsonStr, Tree.class);
}
public static void main(String[] args) {
Tree tree = metaTree(trees, 100);
/**
* Tree@6073f712[id=100,pId=1,key=root,value=root,childList=[
* Tree(id=1000, pId=100, key=node1, value=node1, childList=[
* Tree(id=1100, pId=1000, key=node11, value=node11, childList=[
* Tree(id=1110, pId=1100, key=node111, value=node111, childList=null),
* Tree(id=1120, pId=1100, key=node112, value=node112, childList=null)]),
* Tree(id=1200, pId=1000, key=node12, value=node12, childList=null)]),
* Tree(id=2000, pId=100, key=node2, value=node2, childList=[
* Tree(id=2100, pId=2000, key=node21, value=node21, childList=[
* Tree(id=2110, pId=2100, key=node211, value=node21, childList=null)]),
* Tree(id=2200, pId=2000, key=node22, value=node22, childList=null)]),
* Tree(id=3000, pId=100, key=node3, value=node3, childList=null)]]
*/
System.out.println("tree:" + ToStringBuilder.reflectionToString(tree));
}
private static Tree metaTree(List<Tree> treeList, Integer id) {
//此处getId getPId根据自己实际情况更改
Tree treeConfig = treeList.stream().filter(tree -> tree.getId().equals(id)).collect(Collectors.toList()).get(0);
Map<Integer, List<Tree>> collect = treeList.stream().filter(type -> type.getPId() != null).collect(Collectors.groupingBy(Tree::getPId));
if (collect != null && collect.size() > 0) {
recursion(collect, treeConfig);
}
return treeConfig;
}
private static Tree recursion(Map<Integer, List<Tree>> maps, Tree tree) {
if (tree.getChildList() == null) {
if (maps.get(tree.getId()) != null) {
tree.setChildList(maps.get(tree.getId()));
maps.remove(tree.getId());
if (maps.size() > 0) {
recursion(maps, tree);
}
}
} else {
List<Tree> metaTypeList = tree.getChildList();
if (metaTypeList != null && metaTypeList.size() > 0) {
for (Tree meta : metaTypeList) {
recursion(maps, meta);
}
}
}
return tree;
}
}
来源:https://www.cnblogs.com/javashare/p/9814415.html
标签:Java,list,id,节点
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用AOP的@Around后无返回值的解决
2023-11-24 13:04:09
Java使用Tess4J实现图像识别方式
2022-10-07 19:24:11
![](https://img.aspxhome.com/file/2023/9/60659_0s.png)
c#基数排序Radix sort的实现方法
2021-07-25 02:02:21
Java Maven依赖传递,可选依赖,排除依赖详解
2022-06-09 14:24:14
![](https://img.aspxhome.com/file/2023/9/66489_0s.png)
利用Java生成带有文字的二维码
2022-05-21 15:01:38
基于Luhn算法的银行卡校验规则
2022-06-05 00:56:15
SpringBoot Mybatis Plus公共字段自动填充功能
2022-09-01 12:22:33
一文详解gRPC快速整合SpringCloud
2021-12-11 05:01:02
![](https://img.aspxhome.com/file/2023/4/70504_0s.jpg)
spring boot + mybatis实现动态切换数据源实例代码
2021-11-14 03:37:59
Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler)
2021-09-12 09:01:13
![](https://img.aspxhome.com/file/2023/1/77361_0s.png)
Springboot如何利用拦截器拦截请求信息收集到日志详解
2023-03-09 02:30:57
浅谈java中对集合对象list的几种循环访问
2022-02-11 02:32:33
深入解析Java的Spring框架中bean的依赖注入
2023-12-20 18:50:52
![](https://img.aspxhome.com/file/2023/8/66188_0s.png)
C# 透明窗体制作实现方法比较分析
2022-11-26 02:35:30
![](https://img.aspxhome.com/file/2023/9/77269_0s.gif)
SpringBoot整合XxlJob分布式任务调度平台
2022-07-09 09:47:37
![](https://img.aspxhome.com/file/2023/5/84165_0s.png)
Android下拉刷新以及GridView使用方法详解
2022-12-26 18:01:24
SrpingDruid数据源加密数据库密码的示例代码
2021-06-21 03:26:26
![](https://img.aspxhome.com/file/2023/1/63021_0s.png)
c# 类成员的可访问性代码详解
2022-03-14 22:43:59
![](https://img.aspxhome.com/file/2023/8/87018_0s.png)
在C#中global关键字的作用及其用法
2021-12-24 04:33:19
FeignClient如何通过配置变量调用配置文件url
2023-05-07 08:19:25