树形数据的操作

树形数据的缩进打印

  • 缩进两个空格输出结果
    效果图:
    在这里插入图片描述
  • 代码如下
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author lkp
 * @describe
 * @date 2023/10/16
 */
public class Test{
    static class Node {
        int id;
        int parentId;
        String name;
        public Node(int id, int parentId, String name) 				{
            this.id = id;
            this.parentId = parentId;
            this.name = name;
        }
    }


    public static void main(String[] args) {
        List<Node> nodeList = Arrays.asList(
                new Node(1, 0, "AA"),
                new Node(2, 1, "BB"),
                new Node(3, 1, "CC"),
                new Node(4, 3, "DD"),
                new Node(5, 3, "EE"),
                new Node(6, 2, "FF"),
                new Node(7, 2, "GG"),
                new Node(8, 4, "HH"),
                new Node(9, 5, "II"),
                new Node(10, 0, "JJ"),
                new Node(11, 10, "KK"),
                new Node(12, 10, "LL"),
                new Node(13, 12, "MM"),
                new Node(14, 13, "NN"),
                new Node(15, 14, "OO")
        );
        print(nodeList);
    }

    public static void print(List<Node> nodeList) {

        Map<Integer,Node> map = new HashMap<>();
        Map<Integer,List<Node>> reMap = new HashMap<>();

        for ( int i = 0; i < nodeList.size(); i++){
            Node node = nodeList.get(i);
            map.put(node.id,nodeList.get(i));
        }
        for ( int i = 0; i < nodeList.size(); i++){
            Node node = nodeList.get(i);
            if(reMap.get(node.id) == null){
                reMap.put(node.id,new ArrayList<>());
                if (reMap.get(node.parentId) != null){
                    reMap.get(node.parentId).add(node);
                }
            }
        }
        for (int i = 0; i < nodeList.size(); i++) {
            Node node = nodeList.get(i);
            if(map.get(node.parentId)==null){
                System.out.println(node.name);
                List<Node> nodes = reMap.get(node.id);
                soutResult(nodes,"  ",reMap);
            }
        }
    }

    private static void soutResult(List<Node> nodes,String str,Map<Integer,List<Node>> reMap) {
        for (int i = 0; i < nodes.size(); i++) {
            System.out.println(str+nodes.get(i).name);
            List<Node> nodelist = reMap.get(nodes.get(i).id);
            if (!CollectionUtils.isEmpty(nodelist)){
                soutResult(nodelist,str+"  ",reMap);
            }
        }
    }

}

树形数据的封装

  • 实体类一
package com.sz.utils.file.entry;


import lombok.AllArgsConstructor;
import lombok.Data;


/**
 * @author lkp
 * @describe
 * @date 2023/10/16
 */
@Data
@AllArgsConstructor
public class ModelType {
    /**
     * id
     */
    public Integer id;
    /**
     * 名称
     */
 
    public String name;
    /**
     * 父类id
     */
    public Integer pid;
}
  • 实体类二
package com.sz.utils.file.entry;


import lombok.Data;

import java.util.List;

/**
 * @author lkp
 * @describe
 * @date 2023/10/16
 */
@Data
public class ModelTypeDTO {
    /**
     * id
     */
    public Integer id;
    /**
     * 名称
     */
    public String name;
    /**
     * 父类id
     */
    public Integer pid;
    /**
     * 子类
     */
    List<ModelTypeDTO> children;

}
import com.sz.utils.file.entry.ModelType;
import com.sz.utils.file.entry.ModelTypeDTO;

import java.util.*;

/**
 * @author lkp
 * @describe
 * @date 2023/10/16
 */
public class DataOfTree {
    public static void main(String[] args) {
        List<ModelType> list = new ArrayList<>();
        list.add(new ModelType(3, "机械键盘", 2));
        list.add(new ModelType(4, "薄膜键盘", 2));
        list.add(new ModelType(5, "主题键盘", 2));
        list.add(new ModelType(6, "鼠标", 1));
        list.add(new ModelType(7, "有线鼠标", 6));
        list.add(new ModelType(8, "无线鼠标", 6));
        list.add(new ModelType(10, "小米", 9));
        list.add(new ModelType(11, "华为", 9));
        list.add(new ModelType(12, "红米", 10));
        list.add(new ModelType(13, "荣耀", 11));
        list.add(new ModelType(14, "畅想", 11));
        list.add(new ModelType(1, "数码", null));
        list.add(new ModelType(2, "键盘", 1));
        list.add(new ModelType(9, "手机", null));
        List<ModelTypeDTO> modelTypeVos = toTree(list);
        System.out.println(modelTypeVos);
    }

    /**
     * 将list集合封装成树形结构数据
     *
     * @param list
     * @return
     */
    static List<ModelTypeDTO> toTree(List<ModelType> list) {
        //创建一个map用于存放 <类型id,将类型对象封装成的 ModelTypeVo 对象>
        HashMap<Integer, ModelTypeDTO> map = new HashMap<Integer, ModelTypeDTO>();
        List<ModelTypeDTO> trees = new ArrayList<ModelTypeDTO>();
        for (ModelType type : list) {
            ModelTypeDTO node = new ModelTypeDTO();
            node.setId(type.getId());
            node.setName(type.getName());
            node.setPid(type.getPid());
            map.put(type.getId(), node);
        }
        for (ModelType type : list) {
            Integer pid = type.getPid();
            Integer id = type.getId();
            ModelTypeDTO pNode = map.get(pid);
            ModelTypeDTO cNode = map.get(id);
            //判断map是否存在此pid
            if (map.containsKey(pid)) {
                //存在,则将该pid的子节点取出来
                List<ModelTypeDTO> children = pNode.getChildren();
                if (children == null) {
                    //如果子节点为空,初始化子节点
                    children = new ArrayList<ModelTypeDTO>();
                    pNode.setChildren(children);
                }
                //将此子节点放入对应父节点下
                children.add(cNode);
            } else {
                trees.add(cNode);
            }
        }
        return trees;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值