递归查询所有下级部门树形结构反向递归获取所有ID集合

1 篇文章 0 订阅

mysql递归搜索再之前得原创文档里已经写明了,这个网上比较多。

直接进入正题:原创手写反递归

 package com.kb.nxccims.common.util;

import java.util.ArrayList;
import java.util.List;
import com.kb.nxccims.expandmodel.UnitVO;
/**
 * @author 叶成浪
 * @time 2018年7月12日 - 上午11:06:06
 * @email yechenglang521@163.com
 **/
public class UnitRecursionUtils {
 private UnitRecursionUtils() {
 }
 private static List<Long> rList = null;
 private static UnitRecursionUtils unitRecursionUtils = null;
 public static List<Long> getInstance(List<UnitVO> list) {
  if (rList == null) {
   rList = new ArrayList<Long>();
  }
  synchronized (rList) {
   unitRecursionUtils = new UnitRecursionUtils();
   return unitRecursionUtils.getUnitList(list);
  }
 }
 // YCL递归查询下级单位ID
 private List<Long> getUnitList(List<UnitVO> list) {
  if (!rList.isEmpty()) {
   return rList;
  }
  for (int i = 0; i < list.size(); i++) {
   rList.add(list.get(i).getId());
   uVO(list.get(i).getuList());
  }
  return rList;
 }
 // YCL递归循环下级所有ID
 private static List<UnitVO> uVO(List<UnitVO> uvo) {
  List<UnitVO> list2 = new ArrayList<UnitVO>();
  for (int i = 0; i < uvo.size(); i++) {
   rList.add(uvo.get(i).getId());
   if (uvo.get(i).getuList().isEmpty()) {
    return null;
   } else {
    list2.addAll(uvo.get(i).getuList());
   }
  }
  if (uvo.size() <= 0) {
   return null;
  }
  return uVO(list2);
 }
}

原创版权,未经允许,不得转载。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值