打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
简洁常用权限系统的设计与实现(三):维护和利用节点的深度level,迭代实现树的构造


  1. public static List<Map<String, Object>> buildTree(List<TreeNode> list) {  
  2.   
  3. List<Map<String, Object>> rootList = new ArrayList<Map<String, Object>>();  
  4.   
  5. Map<String, Map<String, Object>> rootMap = new HashMap<String, Map<String, Object>>();  
  6.   
  7. for (TreeNode node : list) {  
  8.   
  9. Integer acl = node.getAcl();  
  10.   
  11. Integer parentAcl = node.getParentAcl();  
  12.   
  13. Map<String, Object> newNode = createNode(acl);  
  14.   
  15. rootMap.put(acl + "", newNode);  
  16.   
  17. if (parentAcl.equals(-1)) {  
  18.   
  19. rootList.add(newNode);  
  20.   
  21. } else {  
  22.   
  23. Map<String, Object> fatherNode = rootMap.get(parentAcl + "");  
  24.   
  25. addChild(fatherNode, newNode);  
  26.   
  27. }  
  28.   
  29. }  
  30.   
  31. return rootList;  
  32.   
  33. }  
  34.   
  35.   
  36.   
  37. private static void addChild(Map<String, Object> father,  
  38.   
  39. Map<String, Object> child) {  
  40.   
  41. //如果father=null,一定是没有正确按照level排序导致的  
  42.   
  43. if (father == null) {  
  44.   
  45. System.out.println("error,Father is null");  
  46.   
  47. return;  
  48.   
  49. }  
  50.   
  51. Object o = father.get("children");  
  52.   
  53. List<Map<String, Object>> childs;  
  54.   
  55. if (o == null) {  
  56.   
  57. childs = new ArrayList<Map<String, Object>>();  
  58.   
  59. father.put("children", childs);  
  60.   
  61. } else {  
  62.   
  63. childs = (List<Map<String, Object>>) o;  
  64.   
  65. }  
  66.   
  67. childs.add(child);  
  68.   
  69. }  

 只需要简单说几点:
1.按照level升序,顶级节点在最前面。
2.把每一个节点的父结点,先放进map。轮到子结点的时候,把它放进父结点的children中。
3.integer比较的时候, 用queals方法,而不是==。

4.Map的key是区分类型的。

map.put(1,"a");

map.get("1");

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
游戏类库-搜索算法
探讨instanceof实现原理,并用两种方法模拟实现 instanceof
基于c语言实现的二叉查找树
【面试现场】如何在500w个单词中统计特定前缀的单词有多少个?
C# TreeView 树拖拽
C#?TreeviewNode的拖动,Node节点上下移动
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服