Java中如何將list轉(zhuǎn)為樹形結(jié)構(gòu)
在系統(tǒng)開發(fā)過程中,可能會碰到一些需求,需要構(gòu)建樹形結(jié)構(gòu),數(shù)據(jù)庫一般就使用父id來表示,比如構(gòu)建菜單、構(gòu)建地區(qū)級聯(lián)、構(gòu)建部門層級結(jié)構(gòu)等等。雖然可以通過數(shù)據(jù)庫SQL查詢,但我們一般都是通過SQL一次性查詢出所有數(shù)據(jù),在程序中處理成樹形結(jié)構(gòu)。本文講述如何將一個List<T> 處理成想要的樹形結(jié)構(gòu)。
1、自己測試過沒問題的:
/** * 對象List轉(zhuǎn)為Tree樹形結(jié)構(gòu) * * @param entityList 傳進來的泛型List * @param primaryFieldName 主鍵名稱 * @param parentFieldName 父級字段名稱 * @return */ public final List<Map<String, Object>> listToTree(List<Map<String, Object>> entityList, String primaryFieldName, String parentFieldName) { //返回的map Tree樹形結(jié)構(gòu) List<Map<String, Object>> treeMap = new ArrayList<>(); //將傳進的參數(shù)entityList轉(zhuǎn)為MapList List<Map<String, Object>> listMap = JSON.parseObject(JSON.toJSONString(entityList), List.class); //聲明一個map用來存listMap中的對象,key為對象id,value為對象本身 Map<String, Map<String, Object>> entityMap = new Hashtable<>(); //循環(huán)listMap把map對象put到entityMap中去 listMap.forEach(map -> entityMap.put(map.get(primaryFieldName).toString(), map)); //循環(huán)listMap進行Tree樹形結(jié)構(gòu)組裝 listMap.forEach(map -> { //獲取map的pid Object pid = map.get(parentFieldName); //判斷pid是否為空或者為0,為空說明是最頂級,直接add到返回的treeMap中去 if (pid == null || StringUtils.equals(pid.toString(), "0")) { treeMap.add(map); } else { //如果pid不為空也不為0,是子集 // 根據(jù)當前map的pid獲取上級 parentMap Map<String, Object> parentMap = entityMap.get(pid.toString()); if (parentMap == null) { //如果parentMap為空,則說明當前map沒有父級,當前map就是頂級 treeMap.add(map); } else { //如果parentMap不為空,則當前map為parentMap的子級 //取出parentMap的所有子級的List集合 List<Map<String, Object>> children = (List<Map<String, Object>>) parentMap.get("children"); if (children == null) { //判斷子級集合是否為空,為空則新創(chuàng)建List children = new ArrayList<>(); parentMap.put("children", children); } //把當前map對象add到parentMap的子級List中去 children.add(map); /** * 因為parentMap是從entityMap中g(shù)et出來的, * 而entityMap中的value又是來自于listMap對象, * 所以parentMap和entityMap中的value的地址都是指向listMap中的對象, * 所以parentMap的children和entityMap中的value的children改變時,都會改變listMap中的對象, * 這里涉及到了地址、指針,就不多說了。 */ } } }); return treeMap; }
2、for 方法轉(zhuǎn)樹形
public class TreeTest { public static void main(String[] args) { List<Tree> node = forMethod(treeList); System.out.println(node); } ? /** * 雙重for循環(huán)方法轉(zhuǎn)換成樹形結(jié)構(gòu) * @param treeList * @return */ public static List<Tree> forMethod(List<Tree> treeList) { List<Tree> rootTree = new ArrayList<>(); for (Tree tree : treeList) { // 第一步 篩選出最頂級的父節(jié)點 if (0 == tree.getParentId()) { rootTree.add(tree); } // 第二步 篩選出該父節(jié)點下的所有子節(jié)點列表 for (Tree node : treeList) { if (node.getParentId().equals(tree.getId())) { if (CollectionUtils.isEmpty(tree.getChildren())) { tree.setChildren(new ArrayList<>()); } tree.getChildren().add(node); } } } return rootTree; } }
3、遞歸方法轉(zhuǎn)樹形
public class TreeTest { public static void main(String[] args) { List<Tree> node = recursionMethod(treeList); System.out.println(node); } /** * 遞歸方法轉(zhuǎn)換成樹形結(jié)構(gòu) * @param treeList * @return */ public static List<Tree> recursionMethod(List<Tree> treeList) { List<Tree> trees = new ArrayList<>(); for (Tree tree : treeList) { // 找出父節(jié)點 if (0 == tree.getParentId()) { // 調(diào)用遞歸方法填充子節(jié)點列表 trees.add(findChildren(tree, treeList)); } } return trees; } ? /** * 遞歸方法 * @param tree 父節(jié)點對象 * @param treeList 所有的List * @return */ public static Tree findChildren(Tree tree, List<Tree> treeList) { for (Tree node : treeList) { if (tree.getId().equals(node.getParentId())) { if (tree.getChildren() == null) { tree.setChildren(new ArrayList<>()); } // 遞歸 調(diào)用自身 tree.getChildren().add(findChildren(node, treeList)); } } return tree; } }
4、stream方法轉(zhuǎn)樹形
public class TreeTest { public static void main(String[] args) { List<Tree> node = recursionMethod(treeList); System.out.println(node); } /** * stream方法轉(zhuǎn)換成樹形結(jié)構(gòu) * @param treeList * @return */ public static List<Tree> streamMethod(List<Tree> treeList) { List<Tree> list = treeList.stream() // 篩選出父節(jié)點 .filter(t -> t.getParentId() == 0) // 設(shè)置父節(jié)點的子節(jié)點列表 .map(item -> {item.setChildren(streamGetChildren(item, treeList)); return item;}) .collect(Collectors.toList()); return list; } ? /** * stream 方式遞歸查找子節(jié)點列表 * @return */ public static List<Tree> streamGetChildren(Tree tree, List<Tree> treeList) { List<Tree> list = treeList.stream() .filter(t -> t.getParentId().equals(tree.getId())) .map(item -> {item.setChildren(streamGetChildren(item, treeList)); return item;}) .collect(Collectors.toList()); return list; } }
5、stream 轉(zhuǎn)樹形優(yōu)化
// 第一種優(yōu)化,我們合并上述兩個方法的相同部分 public class TreeTest { public static void main(String[] args) { List<Tree> node = streamMethod(0, treeList); System.out.println(node); } ? /** * stream 方法轉(zhuǎn)換樹形結(jié)構(gòu)方法的優(yōu)化 * @param parentId * @param treeList * @return */ public static List<Tree> streamMethod(Integer parentId, List<Tree> treeList) { List<Tree> list = treeList.stream() // 篩選父節(jié)點 .filter(t -> t.getParentId().equals(parentId)) // 遞歸設(shè)置子節(jié)點 .map(item -> { item.setChildren(streamMethod(item.getId(), treeList)); return item; }) .collect(Collectors.toList()); return list; } } // 第二種優(yōu)化,只是寫法的不同,核心思路不變 public class TreeTest { public static void main(String[] args) { List<Tree> node = streamMethod(0, treeList); System.out.println(node); } /** * stream 方法轉(zhuǎn)換樹形結(jié)構(gòu)方法的優(yōu)化 * @param parentId * @param treeList * @return */ public static List<Tree> streamMethod(Integer parentId, List<Tree> treeList) { List<Tree> list = new ArrayList<>(); Optional.ofNullable(treeList).orElse(new ArrayList<>()) .stream() // 第一次篩選出主父節(jié)點列表進入循環(huán),循環(huán)里面 進入遞歸 篩選出遞歸傳遞的從父節(jié)點列表 .filter(root -> root.getParentId().equals(parentId)) // 遞歸,最末的父節(jié)點從整個列表篩選出它的子節(jié)點列表依次組裝 .forEach(tree -> { List<Tree> children = streamMethod(tree.getId(), treeList); tree.setChildren(children); list.add(tree); }); return list; } }
經(jīng)過上面的代碼,我們已經(jīng)成功的將 List<T> 轉(zhuǎn)換成了我們想要的樹形結(jié)構(gòu)了,但是此時有需求,要求我們文件夾名稱后面附帶文件夾包含的文件數(shù)量,
數(shù)據(jù)庫或者構(gòu)造的集合數(shù)據(jù)中的文件數(shù)量只統(tǒng)計了文件夾本身下面的文件數(shù)量,并沒有加上子文件夾里面的所有文件夾數(shù)量。我們需要將子文件夾的 count 屬性逐
級累加到父節(jié)點的 count 屬性中更新它。
public class TreeTest { public static void main(String[] args) { // treeList 是已經(jīng)處理好的 樹形結(jié)構(gòu) 集合 countHandler(treeList); System.out.println(treeList); } ? /** * 遞歸將子節(jié)點屬性值累加給父節(jié)點 * @param treeList * @return */ private int countHandler(List<Tree> treeList) { int count = 0; if(CollectionUtil.isEmpty(treeList)){ return count; } for (Tree tree : treeList) { count += tree.getCount(); if (CollectionUtil.isEmpty(tree.getChildren())) { continue; } count += countHandler(tree.getChildren()); tree.setCount(count); if (tree.getParentId() == null || tree.getParentId() == 0) { count = 0; } } return count; } }
到此這篇關(guān)于Java中將list轉(zhuǎn)為樹形結(jié)構(gòu)的文章就介紹到這了,更多相關(guān)java list轉(zhuǎn)樹形結(jié)構(gòu)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java.lang.NullPointerException 如何處理空指針異常的實現(xiàn)
這篇文章主要介紹了java.lang.NullPointerException 如何處理空指針異常的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Springboot實現(xiàn)ModbusTCP通信的示例詳解
ModbusTCP協(xié)議是Modbus由MODICON公司于1979年開發(fā),是一種工業(yè)現(xiàn)場總線協(xié)議標準,本文主要介紹了Springboot實現(xiàn)ModbusTCP通信的相關(guān)知識,需要的可以參考下2023-12-12logback的AsyncAppender高效日志處理方式源碼解析
這篇文章主要為大家介紹了logback的AsyncAppender高效日志處理方式源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10