Java 使用Map实现树形菜单

package com.test;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TreeVo {
    /**
     * 节点id
     */
    private int id;
    /**
     * 节点名称
     */
    private String name;
    /**
     * 父节点id
     */
    private int parent;

    /**
     * 子节点集合
     */
    private List children;

    public static List allTreeVoList() {

        List list = new ArrayList<>();

        TreeVo yunChengTreeVo = TreeVo.builder().id(2).name("运城市").parent(0).build();
        list.add(yunChengTreeVo);
        TreeVo linFenTreeVo = TreeVo.builder().id(3).name("临汾市").parent(0).build();
        list.add(linFenTreeVo);
        TreeVo taiYuanTreeVo = TreeVo.builder().id(4).name("太原市").parent(0).build();
        list.add(taiYuanTreeVo);
        TreeVo shuShanTreeVo = TreeVo.builder().id(5).name("北城区").parent(2).build();
        list.add(shuShanTreeVo);
        TreeVo yaoHaiTreeVo = TreeVo.builder().id(6).name("东城区").parent(2).build();
        list.add(yaoHaiTreeVo);

        TreeVo bengShanTreeVo = TreeVo.builder().id(7).name("小尧区").parent(3).build();
        list.add(bengShanTreeVo);
        TreeVo huaiShangTreeVo = TreeVo.builder().id(8).name("小邻区").parent(3).build();
        list.add(huaiShangTreeVo);

        list.add(TreeVo.builder().id(9).name("小店区").parent(4).build());

        list.add(TreeVo.builder().id(10).name("杏花区").parent(4).build());


        return list;
    }


    public static void main(String[] args) {
        try {

            List list = TreeVo.allTreeVoList();
            if (!CollUtil.isEmpty(list)) {
                //1、过滤根节点
                List TreeVoList = list.stream().filter(vo -> vo.getParent() == 0).collect(Collectors.toList());
                //2、将树所有数据转为map,遍历所有节点
                Map treeVoMap = list.stream().collect(Collectors.toMap(TreeVo::getId, e -> e));
                for (Map.Entry integerTreeVoEntry : treeVoMap.entrySet()) {
                    //2.1取到当前节点
                    TreeVo treeVo = integerTreeVoEntry.getValue();
                    int parentId = treeVo.getParent();
                    //2.2获取当前节点的父节点
                    TreeVo treeVoParent = treeVoMap.get(parentId);
                    //如果是根节点,则跳过根节点
                    if (ObjectUtil.isEmpty(treeVoParent)) {
                        continue;
                    }
                    //2.3将当前节点放入父节点的Children集合中
                    List childrenList = treeVoParent.getChildren();
                    if (ObjectUtil.isEmpty(childrenList)) {
                        //2.3.1每新增一层,都要new ArrayList<>()
                        List treeVoList = new ArrayList<>();
                        treeVoList.add(treeVo);
                        treeVoParent.setChildren(treeVoList);
                    } else {
                        //2.3.2同层之间,在一个list中新增
                        childrenList.add(treeVo);
                    }
                }
                //3 最后返回tempList即可
                System.out.println("====newList===="+ JSONUtil.toJsonStr(TreeVoList));
                System.out.println("=====oldList====="+JSONUtil.toJsonStr(list));
            }

        } catch (Exception e) {
             e.printStackTrace();
        }
    

    }

}

你可能感兴趣的:(java,jvm,开发语言)