久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java list如何根據(jù)id獲取子節(jié)點

瀏覽:18日期:2022-09-04 09:07:58

工作中因業(yè)務(wù)需求,將數(shù)據(jù)庫中的樹狀結(jié)構(gòu)的數(shù)據(jù)根據(jù)父節(jié)點獲取所有的子節(jié)點

實現(xiàn)思路

1.獲取整個數(shù)據(jù)的list集合數(shù)據(jù)

2.將數(shù)據(jù)分組,java8 list有g(shù)roupby分組,java8之前的自己遍歷整理

3.分組后遞歸獲取子節(jié)點,有子節(jié)點的添加,沒有的設(shè)置子節(jié)點并刪除分組的數(shù)據(jù),知道分組數(shù)據(jù)刪完

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根據(jù)自己實際情況更改 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; }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 丽江市| 宝清县| 玉树县| 南陵县| 河间市| 五莲县| 南川市| 乐东| 张北县| 华坪县| 哈尔滨市| 大悟县| 安丘市| 富锦市| 疏附县| 曲麻莱县| 肥西县| 白河县| 邵武市| 富锦市| 宁阳县| 从江县| 沂源县| 中宁县| 金塔县| 亳州市| 塔城市| 商丘市| 井研县| 奈曼旗| 衡东县| 兴业县| 五家渠市| 乃东县| 壶关县| 泊头市| 蒲城县| 蓬安县| 遂昌县| 平陆县| 无极县|