实现文件目录结构功能

简介: 实现文件目录结构功能

@[TOC]

说明:该文章用于目录结构递进显示

image.png

Node

package com.geespace.microservices.directory.assets.entity;

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

import lombok.Data;

/**
 * @Author: wjq
 * @Date: 2021-03-14 13:46
 * @Version 1.0
 */
@Data
public class Node {
    /**
     * 名称
     */
    private String name;
    /**
     * id
     */
    private Long id;
    /**
     * pid
     */
    private Long pid;
    /**
     * 子目录
     */
    private List<Node> children = new ArrayList();
}

ConstructTree

package com.geespace.microservices.directory.assets.util;

import java.util.ArrayList;
import java.util.HashMap;

import com.alibaba.fastjson.JSONObject;
import com.geespace.microservices.directory.assets.dto.DirectoryAssetsDto;
import com.geespace.microservices.directory.assets.entity.Node;

import org.springframework.stereotype.Component;

/**
 * @Author: wjq
 * @Date: 2021-03-12 16:39
 * @Version 1.0
 */
@Component
public class ConstructTree {
    public static final int ROOTPID = -1;
    public static final int CAPACITY = 100;

    /**
     * constructLinks
     *
     * @param directoryList directoryList
     * @return 结果
     */
    public Node constructLinks(ArrayList<DirectoryAssetsDto> directoryList) {
        Long rootId = null;
        HashMap<Long, Node> nodeMap = new HashMap<>(CAPACITY);
        for (DirectoryAssetsDto directoryInfo:directoryList) {
            Node tempNode = new Node();
            tempNode.setId(directoryInfo.getId());
            tempNode.setName(directoryInfo.getName());
            tempNode.setPid(directoryInfo.getPid());
            nodeMap.put(directoryInfo.getId(), tempNode);
            if (directoryInfo.getPid() == ROOTPID) {
                rootId = directoryInfo.getId();
            }
        }
        for (Long key: nodeMap.keySet()) {
            Long pid = nodeMap.get(key).getPid();
            if (pid != ROOTPID) {
                nodeMap.get(pid).getChildren().add(nodeMap.get(key));
            }
        }
        Node root = nodeMap.get(rootId);
        return root;
    }

    /**
     *
     * @param node node
     * @return json
     */
    public JSONObject constructJS(Node node) {
        JSONObject jsonObject = new JSONObject();
        ArrayList<JSONObject> children = new ArrayList<>();
        for (Node child: node.getChildren()) {
            children.add(this.constructJS(child));
        }
        jsonObject.put("children", children);
        jsonObject.put("id", node.getId());
        jsonObject.put("name", node.getName());
        return jsonObject;
    }

    /**
     *
     * @param data data
     * @return jason
     */
    public JSONObject method(ArrayList<DirectoryAssetsDto> data) {
        Node root = this.constructLinks(data);
        return this.constructJS(root);
    }
}
目录
相关文章
|
9月前
|
缓存 关系型数据库 Linux
Linux目录结构:深入理解与命令创建指南
Linux目录结构:深入理解与命令创建指南
170 4
|
9月前
|
Linux
Linux目录结构
【2月更文挑战第19天】Linux目录结构。
47 1
|
9月前
|
安全 Oracle 关系型数据库
Linux目录结构及详细介绍
Linux目录结构及详细介绍
124 0
|
存储 缓存 Linux
哇!真的是你呀~今天我们来学习Linux目录结构与文件管理
在Linux的学习过程中目录结构与文件管理是基础且重要的,要想学好Linux就要了解和掌握这些下面就让我们一起来看看。Linux系统的组成:内核 、shell、用户操作或应用程序、基本构成:用户态、内核态。
44 0
|
C++
19.【c++基础篇.三个文件实现】
19.【c++基础篇.三个文件实现】
92 0
导出项目目录树结构
在cmd中切换到自己所需要导出项目树结构的根目录下,在win中可以使用cd来切换文件夹
196 0
|
安全 Python
【通用文件操作】删除空文件夹
对于有强迫症的人来说,空文件夹是不允许存在的东西,而电脑中文件又特别多,而且空文件夹也不好找,要一个一个删除真的是太麻烦了,今天就和大家分享一下删除空文件夹的操作。文章比较啰嗦,读者可以直接跳到最后一段。
534 0
|
缓存 Linux 开发者
文件目录结构 | 学习笔记
快速学习文件目录结构。
193 0
文件目录结构 | 学习笔记
|
存储 缓存 Linux
Linux目录结构介绍
  根文件系统   /bin   这一目录中存放了供所有用户使用的完成基本维护任务的命令。其中bin是binary的缩写,表示二进制文件,通常为可执行文件。一些常用的系统命令,如cp、ls等保存在该目录中。
129 0
|
前端开发 数据采集 JavaScript
项目目录标准结构
(学习自慕课网中《所向披靡的响应式开发》课程中) 一.如何组织项目目录结构 1.约定优于配置 2.约定代码结构或命名规范来减少配置数量 (没有最好的组织方式,只有合适的组织方式) 样式放入CSS文件下,main.css是通用样式,normalize.css是引用样式,login是的登录样式。
1490 0