您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java中list根據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:" + 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; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java中list根據id獲取子節點的示例”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。