时间:2021-05-20
工作中因业务需求,将数据库中的树状结构的数据根据父节点获取所有的子节点
实现思路
1.获取整个数据的list集合数据
2.将数据分组,java8 list有groupby分组,java8之前的自己遍历整理
3.分组后递归获取子节点,有子节点的添加,没有的设置子节点并删除分组的数据,知道分组数据删完
Tree.java
@Datapublic 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; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
获取元素节点的子节点1,获取id的所有子节点varcityNode=document.getElemnetById("")2,利用元素节点的childNodes
本文实例讲述了ztree获取当前选中节点子节点id集合的方法。分享给大家供大家参考。具体分析如下:要求:获取当前选中节点的子节点id集合。步骤:1.获取当前节点
分享下jquery取子节点及当前节点属性值的方法。资料管理取子节点:$(".menulinka").each(function(){varid=this.id;
分享下jquery取子节点及当前节点属性值的方法。资料管理取子节点:$(".menulinka").each(function(){varid=this.id;
使用zTree插件实现树形图中,需要获取当前点击的父节点的子节点数的需求,使用treeNode.children获取子节点数据集合,使用length方法获取集合