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


前言:

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

正文:

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

@Data
public class TreeDto {
    private String id;
    private String name;
    private String pid;
    private String isParent;
    private List<TreeDto> 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);

    }
}

3.然后寫個main方法來測試下

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);
}

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

{
    "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"
}

原文:https://blog.csdn.net/jdk_wangtaida/article/details/87867620 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM