国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
簡(jiǎn)潔常用權(quán)限系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)(四):不維護(hù)level,用遞歸方式構(gòu)造樹(shù)

第三篇中,我們通過(guò)維護(hù)節(jié)點(diǎn)的深度level,通過(guò)迭代所有的節(jié)點(diǎn),只需要一次,就構(gòu)造了樹(shù)。
  本篇,換一種方式。

  好處是:不維護(hù)節(jié)點(diǎn)的深度level,增加和修改節(jié)點(diǎn)時(shí),也不用維護(hù)。遞歸實(shí)現(xiàn),代碼比較清晰。
  壞處是:節(jié)點(diǎn)較多的時(shí)候,性能可能不夠好。不能直接查詢到節(jié)點(diǎn)的深度level。當(dāng)然,如果需要level字段,在遞歸過(guò)程中,是可以計(jì)算得到的。關(guān)于在遞歸過(guò)程中,計(jì)算level,后面有介紹這種方法。

  關(guān)于樹(shù)的遍歷和查找,大家都有基礎(chǔ),上面描述了一些總體思路,代碼中有注釋,基本就不用再詳細(xì)介紹了。

  

  1. //不維護(hù)節(jié)點(diǎn)的深度level,通過(guò)遞歸構(gòu)造樹(shù)  
  2.   
  3.     public static List<Map<String, Object>> buildTree(  
  4.   
  5.             List<Map<String, Object>> list) {  
  6.   
  7.         //目標(biāo)樹(shù)  
  8.   
  9.         List<Map<String, Object>> treeList = new ArrayList<Map<String, Object>>();  
  10.   
  11.         //所有的頂級(jí)節(jié)點(diǎn)  
  12.   
  13.         List<Map<String, Object>> rootList = TreeMenuUtil.findTopLevelNodeList(list);  
  14.   
  15.         //所有的非頂級(jí)節(jié)點(diǎn)  
  16.   
  17.         List<Map<String, Object>> notRootList = TreeMenuUtil.findNotRootList(list);  
  18.   
  19.         //遍歷頂級(jí)節(jié)點(diǎn)  
  20.   
  21.         for (Map<String, Object> root : rootList) {  
  22.   
  23.             // 構(gòu)造子結(jié)點(diǎn)  
  24.   
  25.             buildChildList(root, notRootList);  
  26.   
  27.             //把根節(jié)點(diǎn)放到集合中  
  28.   
  29.             treeList.add(root);  
  30.   
  31.         }  
  32.   
  33.         return treeList;  
  34.   
  35.     }  
  36.   
  37.     // 為一個(gè)“root節(jié)點(diǎn),這個(gè)地方的root指有孩子的節(jié)點(diǎn)”  
  38.   
  39.     private static void buildChildList(Map<String, Object> rootNode,  
  40.   
  41.             List<Map<String, Object>> notRootList) {  
  42.   
  43.         Integer acl =  MapUtils.getInteger(rootNode, "acl");  
  44.   
  45.         for (Map<String, Object> notRoot : notRootList) {  
  46.   
  47.             //從非頂級(jí)節(jié)點(diǎn)中,為當(dāng)前節(jié)點(diǎn)尋找子結(jié)點(diǎn)  
  48.   
  49.             boolean equals =  MapUtils.getInteger(notRoot, "parent_acl").equals(acl);  
  50.   
  51.             if (equals) {  
  52.   
  53.                 List<Map<String, Object>> list = (List<Map<String, Object>>) rootNode  
  54.   
  55.                         .get("children");  
  56.   
  57.                 if (list == null) {  
  58.   
  59.                     list = new ArrayList<Map<String, Object>>();  
  60.   
  61.                     rootNode.put("children", list);  
  62.   
  63.                 }  
  64.   
  65.                 list.add(notRoot);  
  66.   
  67.                 //遞歸,為當(dāng)前節(jié)點(diǎn)構(gòu)造子結(jié)點(diǎn)  
  68.   
  69.                 buildChildList(notRoot, notRootList);  
  70.   
  71.             }  
  72.   
  73.         }  
  74.   
  75.     }  
原文首發(fā):http://fansunion.cn/article/detail/572.html

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
二叉樹(shù)的Python實(shí)現(xiàn)
TreeMap就這么簡(jiǎn)單
Android: ListView排序及過(guò)濾
hibernate調(diào)用存儲(chǔ)過(guò)程及處理返回集合
【轉(zhuǎn)】并查集路徑壓縮
5.3.5 Java中緩存的基本實(shí)現(xiàn)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服