更好的形成树形组织结构图的方法

说明

https://my.oschina.net/u/3866531/blog/2251648

中的方法比较常规,思路还算比较清晰。但是缺点是对数据库层级有依赖,比如如果系统连的是MySQL数据库,那么要用到MySQL系统自带的FIND_IN_SET函数和自己设计的getChildList函数。

如果系统连接的是Oracle数据库,Oracle没有FIND_IN_SET函数,按这个思路还要自己实现FIND_IN_SET函数,然后再实现一遍Oracle下的getChildList函数。

有没有更好的方法呢?能否不那么依赖数据库层面的操作呢?答案是肯定有,就是使用好的算法,将数据从数据库一次取出来后在java层面处理形成树形组织结构图。

示例代码

树节点对象代码

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class RegionBeanTree {
    private String code;
    private String label;
    private String pid;
    private List<RegionBeanTree> children;

}

★工具类

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.richfit.gisrest.util.JsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/26.
 */
public class TreeToolUtils {
    //根节点对象存放到这里
    private List<RegionBeanTree> rootList;
    //其他节点存放到这里,可以包含根节点
    private List<RegionBeanTree> bodyList;


    public TreeToolUtils(List<RegionBeanTree> rootList, List<RegionBeanTree> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public static void main(String[] args)  throws JsonProcessingException {
        //最高节点
        RegionBeanTree beanTree1 = new RegionBeanTree();
        beanTree1.setCode("540000");
        beanTree1.setLabel("西藏省");
        beanTree1.setPid("100000");

        RegionBeanTree beanTree2 = new RegionBeanTree();
        beanTree2.setCode("540100");
        beanTree2.setLabel("拉萨市");
        beanTree2.setPid("540000");
        RegionBeanTree beanTree3 = new RegionBeanTree();
        beanTree3.setCode("540300");
        beanTree3.setLabel("昌都市");
        beanTree3.setPid("540000");
        RegionBeanTree beanTree4 = new RegionBeanTree();
        beanTree4.setCode("540121");
        beanTree4.setLabel("林周县");
        beanTree4.setPid("540100");
        RegionBeanTree beanTree5 = new RegionBeanTree();
        beanTree5.setCode("540121206");
        beanTree5.setLabel("阿朗乡");
        beanTree5.setPid("540121");
        RegionBeanTree beanTree6 = new RegionBeanTree();
        List<RegionBeanTree> rootList = new ArrayList<>();
        rootList.add(beanTree1);
        List<RegionBeanTree> bodyList = new ArrayList<>();
        bodyList.add(beanTree1);
        bodyList.add(beanTree2);
        bodyList.add(beanTree3);
        bodyList.add(beanTree4);
        bodyList.add(beanTree5);
        TreeToolUtils utils = new TreeToolUtils(rootList, bodyList);
        List<RegionBeanTree> result = utils.getTree();
        String json = JsonUtil.toJsonString(result.get(0));
        System.out.println(json);
    }

    //调用的方法入口
    public List<RegionBeanTree> getTree() {

        if (bodyList != null && !bodyList.isEmpty()) {
            //声明一个map,用来过滤已操作过的数据
            Map<String, String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree, map));
            return rootList;
        }
        return null;
    }

    public void getChild(RegionBeanTree beanTree, Map<String, String> map) {
        List<RegionBeanTree> childList = Lists.newArrayList();
        bodyList.stream().filter(c -> !map.containsKey(c.getCode())).filter(c -> c.getPid().equals(beanTree.getCode())).forEach(c -> {
            map.put(c.getCode(), c.getPid());
            getChild(c, map);
            childList.add(c);
        });
        beanTree.setChildren(childList);
    }
}

运行结果

{

  "code" : "540000",

  "label" : "西藏省",

  "pid" : "100000",

  "children" : [ {

    "code" : "540100",

    "label" : "拉萨市",

    "pid" : "540000",

    "children" : [ {

      "code" : "540121",

      "label" : "林周县",

      "pid" : "540100",

      "children" : [ {

        "code" : "540121206",

        "label" : "阿朗乡",

        "pid" : "540121",

        "children" : [ ]

      } ]

    } ]

  }, {

    "code" : "540300",

    "label" : "昌都市",

    "pid" : "540000",

    "children" : [ ]

  } ]

}

说明--可以根据项目需求对节点属性进行扩展,主要是理解其处理思路

转载于:https://my.oschina.net/u/3866531/blog/2251994

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值