Javalist如何根据id获取⼦节点
⼯作中因业务需求,将数据库中的树状结构的数据根据⽗节点获取所有的⼦节点
实现思路
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:" + flectionToString(tree));
}
private static Tree metaTree(List<Tree> treeList, Integer id) {
//此处getId getPId根据⾃⼰实际情况更改
Tree treeConfig = treeList.stream().filter(tree -> Id().equals(id)).List()).get(0);
Map<Integer, List<Tree>> collect = treeList.stream().filter(type -> PId() != null).upingBy(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 (ChildList() == null) {
if ((Id()) != null) {
tree.(Id()));
if (maps.size() > 0) {
recursion(maps, tree);
}
}
} else {
List<Tree> metaTypeList = ChildList();
if (metaTypeList != null && metaTypeList.size() > 0) {
for (Tree meta : metaTypeList) {
recursion(maps, meta);
}
}
}
return tree;
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。