Java代碼實現封裝多級樹結構對象

前言:

在開發中,我們經常見到,前端展示樹狀結構的,這時候就需要後端去封裝一個多級樹結構對象,前端根據這樣結構的數據去渲染數據,這篇文章講的是如何封裝成多級樹結構對象。

正文:

1.先封裝個樹結構的對象

/**樹結構對象*/
public class TreeDto {
    private String id;
    private String name;
    private String pid;
    private String isParent;
    private List<TreeDto> childTreeDto;

    public TreeDto(String id, String name, String pid, String isParent, List<TreeDto> childTreeDto) {
        this.id = id;
        this.name = name;
        this.pid = pid;
        this.isParent = isParent;
        this.childTreeDto = childTreeDto;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getIsParent() {
        return isParent;
    }

    public void setIsParent(String isParent) {
        this.isParent = isParent;
    }

    public List<TreeDto> getChildTreeDto() {
        return childTreeDto;
    }

    public void setChildTreeDto(List<TreeDto> childTreeDto) {
        this.childTreeDto = childTreeDto;
    }
}

 2.然後我把工具類代碼粘貼下

public class TreeToolUtils {
    private List<TreeDto> rootList; //根節點對象存放到這裏

    private List<TreeDto> bodyList; //其他節點存放到這裏,可以包含根節點

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

    public List<TreeDto> 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(TreeDto treeDto, Map<String,String> map){
        List<TreeDto> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getId()))
                .filter(c ->c.getPid().equals(treeDto.getId()))
                .forEach(c ->{
                    map.put(c.getId(),c.getPid());
                    getChild(c,map);
                    childList.add(c);
                });
        treeDto.setChildTreeDto(childList);

    }


    /**測試*/
    public static void main(String[] args) {
        TreeDto treeDto = new TreeDto("1", "總店", "null", "true",null);
        TreeDto treeDto1 = new TreeDto("2", "市分店", "1", "true",null);
        TreeDto treeDto2 = new TreeDto("3", "縣分店", "2", "true",null);
        TreeDto treeDto3 = new TreeDto("710", "店長", "3", "true",null);
        TreeDto treeDto4= new TreeDto("713", "財務部", "3", "true",null);
        TreeDto treeDto5 = new TreeDto("20032", "後勤部", "3", "true",null);
        TreeDto treeDto6 = new TreeDto("1909", "小王", "710", "false",null);
        TreeDto treeDto7= new TreeDto("1974", "小張", "713", "false",null);
        TreeDto treeDto8 = new TreeDto("388187", "佳佳", "20032", "false",null);
        TreeDto treeDto9 = new TreeDto("1949", "阿達", "20032", "false",null);
        ArrayList<TreeDto> rootList = new ArrayList<>();//根節點
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子節點
        rootList.add(treeDto);
        bodyList.add(treeDto1);
        bodyList.add(treeDto2);
        bodyList.add(treeDto3);
        bodyList.add(treeDto4);
        bodyList.add(treeDto5);
        bodyList.add(treeDto6);
        bodyList.add(treeDto7);
        bodyList.add(treeDto8);
        bodyList.add(treeDto9);
        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);
        List<TreeDto> result =  utils.getTree();
        String jsonString = JSONObject.toJSONString(result.get(0));
        System.out.println(jsonString);
    }


}

3.最後控制檯打印出的結果格式化後,就是這樣的數據啦,前端根據層級去渲染數據就行啦

{
    "childTreeDto": [{
        "childTreeDto": [{
            "childTreeDto": [{
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "1909",
                    "isParent": "false",
                    "name": "小王",
                    "pid": "710"
                }],
                "id": "710",
                "isParent": "true",
                "name": "店長",
                "pid": "3"
            }, {
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "1974",
                    "isParent": "false",
                    "name": "小張",
                    "pid": "713"
                }],
                "id": "713",
                "isParent": "true",
                "name": "財務部",
                "pid": "3"
            }, {
                "childTreeDto": [{
                    "childTreeDto": [],
                    "id": "388187",
                    "isParent": "false",
                    "name": "佳佳",
                    "pid": "20032"
                }, {
                    "childTreeDto": [],
                    "id": "1949",
                    "isParent": "false",
                    "name": "阿達",
                    "pid": "20032"
                }],
                "id": "20032",
                "isParent": "true",
                "name": "後勤部",
                "pid": "3"
            }],
            "id": "3",
            "isParent": "true",
            "name": "縣分店",
            "pid": "2"
        }],
        "id": "2",
        "isParent": "true",
        "name": "市分店",
        "pid": "1"
    }],
    "id": "1",
    "isParent": "true",
    "name": "總店",
    "pid": "null"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章