编程知识 cdmana.com

Java constructs tree structure through map

In development , There are always generals A scenario where data is assembled into a tree structure , In addition to being recursive , You can also use map Assembly implementation .

One , Construct basic data



import apple.laf.JRSUIUtils;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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


/**
 * @ founder  
 * @ Creation time  2020/5/25
 * @ describe 
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TreeVo {

    private int id;
    private String name;
    private int parent;
    private List<TreeVo> children;


    public static List<TreeVo> allTreeVoList() {

        List<TreeVo> list = Lists.newArrayList();
        TreeVo heBeiTreeVo = TreeVo.builder().id(1).name(" hebei ").parent(0).children(Lists.newArrayList()).build();
        list.add(heBeiTreeVo);
        TreeVo hanTreeVo = TreeVo.builder().id(2).name(" handan ").parent(1).children(Lists.newArrayList()).build();
        list.add(hanTreeVo);
        TreeVo weiTreeVo = TreeVo.builder().id(3).name(" Weixian ").parent(2).children(Lists.newArrayList()).build();
        list.add(weiTreeVo);
        TreeVo beiTreeVo = TreeVo.builder().id(4).name(" Beijing ").parent(0).children(Lists.newArrayList()).build();
        list.add(beiTreeVo);
        TreeVo jingTreeVo = TreeVo.builder().id(5).name(" The Beijing municipal ").parent(4).children(Lists.newArrayList()).build();
        list.add(jingTreeVo);
        TreeVo chaoTreeVo = TreeVo.builder().id(6).name(" Chaoyang District ").parent(5).children(Lists.newArrayList()).build();
        list.add(chaoTreeVo);
        return list;
    }
}

Two , adopt map Group traversal

 /**
     *  Assembly tree 
     */
    public void wrapperTreeTest() {
        Gson gson = new Gson();
        List<TreeVo> list = TreeVo.allTreeVoList();
        logger.info(" The result is :{}", gson.toJson(list));
        Map<Integer, TreeVo> treeVoMap = list.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
        // Get the top tree node 
        List<TreeVo> rootList = list.stream().filter(i -> !treeVoMap.containsKey(i.getParent())).collect(toList());
        logger.info("rootList is:{}", gson.toJson(rootList));
        for (TreeVo treeVo : list) {
            if (treeVoMap.containsKey(treeVo.getParent())) {
                TreeVo parentTree = treeVoMap.get(treeVo.getParent());
                List<TreeVo> children = parentTree.getChildren();
                children.add(treeVo);
            }
        }
        logger.info(" Finally, the tree structure is :{}", gson.toJson(rootList));
    }

3、 ... and , The result is

[
    {
        "id":1,
        "name":" hebei ",
        "parent":0,
        "children":[
            {
                "id":2,
                "name":" handan ",
                "parent":1,
                "children":[
                    {
                        "id":3,
                        "name":" Weixian ",
                        "parent":2,
                        "children":[

                        ]
                    }
                ]
            }
        ]
    },
    {
        "id":4,
        "name":" Beijing ",
        "parent":0,
        "children":[
            {
                "id":5,
                "name":" The Beijing municipal ",
                "parent":4,
                "children":[
                    {
                        "id":6,
                        "name":" Chaoyang District ",
                        "parent":5,
                        "children":[

                        ]
                    }
                ]
            }
        ]
    }
]

 

版权声明
本文为[osc_ s9cni3go]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225085210306r.html

Scroll to Top