JAVA樹形結構 通用程式碼(高效能)

四道街硬漢發表於2018-11-17
package com.test.main;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

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 List<RegionBeanTree> getTree(){   //呼叫的方法入口
        if(bodyList != null && !bodyList.isEmpty()){
            //宣告一個map,用來過濾已操作過的資料
            Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));//傳遞根物件和一個空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()))//map內不包含子節點的code
                .filter(c ->c.getPid().equals(beanTree.getCode()))//子節點的父id==根節點的code 繼續迴圈
                .forEach(c ->{
                    map.put(c.getCode(),c.getPid());//當前節點code和父節點id
                    getChild(c,map);//遞迴呼叫
                    childList.add(c);
                });
        beanTree.setChildren(childList);
    }

    public static void main(String[] args){
        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();
        result.get(0);
    }
}

 

 

package com.test.main;

import java.util.List;

public class RegionBeanTree {
    private String code ;
    private String pid ;
    private String label ;
    private List<RegionBeanTree> children;
}

一位不知名的大神寫的,收藏下

相關文章