组织树列表查询,不使用递归一次循环搞定

本文介绍了一种在不使用递归的情况下查询树形结构的方法,通过将树转换为Map集合,实现对树的遍历和排序,避免了递归可能导致的栈溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:不使用递归查询出一棵树。

service层业务代码,35行以前是关键,后面只是排序操作。

 1 @SuppressWarnings("unchecked")
 2     public JsonRet getOrganizationList(HttpServletRequest request, HttpServletResponse response) {
 3         LOG.d("getOrgnazationList");
 4         // 从Session得到用户信息
 5          HttpSession session = request.getSession();
 6         TuUser tuUser = (TuUser) session.getAttribute(Constant.USER_INFO);
 7         List<TuOrganization> organizationList = tuOrganizationMapper.getOrganizationList(tuUser.getBranchNo());
 8 
 9         Map<String, Object> item;
10         //获取根节点的集合
11         List<Map<String, Object>> root = new ArrayList<>();
12         Map<String, Map<String, Object>> tempMap = new HashMap<>();
13         //子节点集合
14         List<Map<String, Object>> children;
15         for(TuOrganization o : organizationList) {
16             children = new ArrayList<>();
17             item = new HashMap<>();
18             item.put("id", o.getId());
19             item.put("parentId", o.getParentId());
20             item.put("created", o.getCreateTime());
21             item.put("name", o.getName());
22             item.put("sort", o.getSort());
23             item.put("status", o.getStatus());
24             item.put("children", children);
25             //每次循环都将集合重新循环添加到父集合中
26             tempMap.put(o.getId(), item);
27             Map<String, Object> parent = tempMap.get(o.getParentId());
28             if(null != parent) {
29                 //父集合不为空时,获取下级集合
30                 ((List<Map<String, Object>>)parent.get("children")).add(item);
31             } else {
32                 //根集合
33                 root.add(item);
34             }
35         }
36 
37         Comparator<Map<String, Object>> comparator = new Comparator<Map<String, Object>>() {
38             @Override
39             public int compare(Map<String, Object> o1, Map<String, Object> o2) {
40                 //根据sort字段进行排序
41                 Integer i1 = ((Integer)o1.get("sort"));
42                 Integer i2 = ((Integer)o2.get("sort"));
43                 if(i1 > i2) {
44                     return 1;
45                 } else if(i1 < i2) {
46                     return -1;
47                 }
48                 //若sort字段相同的话,根据创建时间来进行排序
49                 Date d1 = ((Date)o1.get("created"));
50                 Date d2 = ((Date)o2.get("created"));
51                 if(null == d1 || null == d2) return 0;
52                 return d1.compareTo(d2);
53             }
54         };
55 
56         Collections.sort(root, comparator);
57         for (Map.Entry<String, Map<String, Object>> entry : tempMap.entrySet()) {
58             Collections.sort(((List<Map<String, Object>>)entry.getValue().get("children")), comparator);
59         }
60 
61         return jsonResponse(root, Constants.CODE_SUCCESS, "成功");
62     }

 

在循环内封装tempMap,然后直接就可以调用,是因为在SQL中对path做了排序,越往根级的组织越先被遍历到,或者可以直接遍历两次,第一次用来封装temMap,这样SQL语句中就不需要path排序,getOrganizationList SQL语句如下

  <select id="getOrganizationList" parameterType="java.lang.String" resultMap="BaseResultMap">
   select
    <include refid="Base_Column_List" />
    from tu_organization where branch_no = #{branchNo,jdbcType=VARCHAR} order by path
  </select>

path:组织路径;例:1-2-3 代表当前组织parent是组织2,组织2的parent是组织1,每个branch_no下的path都从1开始,且格式必须为‘1-2-3 ...’

 This idea is from my leader lixiang

转载于:https://www.cnblogs.com/lvtao/p/8683183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值