Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从文件夹对象列表中递归获取绝对路径?_Java_Algorithm - Fatal编程技术网

Java 如何从文件夹对象列表中递归获取绝对路径?

Java 如何从文件夹对象列表中递归获取绝对路径?,java,algorithm,Java,Algorithm,有文件夹对象的列表,我需要将它们连接到绝对路径。每个文件夹都有字段“parentId”(可空)和“orderNumber”,如: 所以,如果parentId==null->folder是绝对路径中的第一个,如果不是->我们应该得到parentfolder,依此类推 我是这样做的,也许有更简单的方法 public static void main(String[] args) { List<Folder> list = new ArrayList<Folder&g

有文件夹对象的列表,我需要将它们连接到绝对路径。每个文件夹都有字段“parentId”(可空)和“orderNumber”,如:

所以,如果parentId==null->folder是绝对路径中的第一个,如果不是->我们应该得到parentfolder,依此类推

我是这样做的,也许有更简单的方法

public static void main(String[] args) {
        List<Folder> list = new ArrayList<Folder>();
        list.add(new Folder(1, null, "root1"));
        list.add(new Folder(3, 2, "child2"));
        list.add(new Folder(2, 1, "child1"));
        list.add(new Folder(5, 4, "child4"));
        list.add(new Folder(6, null, "root2"));
        list.add(new Folder(4, 3, "child3"));

        Integer fromId = 5;
        String path = getAbsolutePath(list, getFolderById(list, fromId));
        System.out.println(path);
    }

    public static String getAbsolutePath(List<Folder> list, Folder folder) {
        String path = "";
        Integer parentId = folder.getParentId();
        if (parentId != null) {
            Folder parent = getFolderById(list, parentId);
            path += getAbsolutePath(list, parent) + "/";
        }
        path += folder.getDesc();
        return path;
    }

    public static Folder getFolderById(List<Folder> list, Integer folderId) {
        return list.stream().filter(row -> row.getId() == folderId).findFirst().get();
    }

Output: root1/child1/child2/child3/child4
publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
添加(新文件夹(1,空,“root1”);
添加(新文件夹(3,2,“child2”);
添加(新文件夹(2,1,“child1”);
添加(新文件夹(5,4,“child4”);
添加(新文件夹(6,空,“root2”);
添加(新文件夹(4,3,“child3”);
整数fromId=5;
字符串路径=getAbsolutePath(列表,getFolderById(列表,fromId));
System.out.println(路径);
}
公共静态字符串getAbsolutePath(列表、文件夹){
字符串路径=”;
整数parentId=folder.getParentId();
if(parentId!=null){
文件夹父项=getFolderById(列表,父项ID);
path+=getAbsolutePath(列表,父级)+“/”;
}
path+=folder.getDesc();
返回路径;
}
公共静态文件夹getFolderById(列表,整数folderId){
返回list.stream().filter(行->行.getId()==folderId).findFirst().get();
}
输出:root1/child1/child2/child3/child4
更新:

public static void main(String[] args) {
        List<Folder> list = new ArrayList<Folder>();
        list.add(new Folder(1, null, "root1"));
        list.add(new Folder(3, 2, "child2"));
        list.add(new Folder(2, 1, "child1"));
        list.add(new Folder(5, 4, "child4"));
        list.add(new Folder(6, null, "root2"));
        list.add(new Folder(4, 3, "child3"));
        Map<Integer, Folder> folderMap = list.stream().collect(Collectors.toMap(
                Folder::getId, folder -> folder
        ));

        Integer fromId = 5;
        String path = getAbsolutePath(folderMap, folderMap.get(fromId));
        System.out.println(path);
    }

    public static String getAbsolutePath(Map<Integer, Folder> folderMap, Folder folder) {
        Folder current = folder;
        List<String> buffer = new ArrayList<>();
        while(true) {
            Integer parentId = current.getParentId();
            buffer.add(current.getDesc());
            if (parentId != null) {
                current = folderMap.get(parentId);
            } else {
                break;
            }
        }
        Collections.reverse(buffer);
        return String.join("/", buffer);
    }
publicstaticvoidmain(字符串[]args){
列表=新的ArrayList();
添加(新文件夹(1,空,“root1”);
添加(新文件夹(3,2,“child2”);
添加(新文件夹(2,1,“child1”);
添加(新文件夹(5,4,“child4”);
添加(新文件夹(6,空,“root2”);
添加(新文件夹(4,3,“child3”);
Map folderMap=list.stream().collect(Collectors.toMap(
文件夹::getId,文件夹->文件夹
));
整数fromId=5;
字符串路径=getAbsolutePath(folderMap,folderMap.get(fromId));
System.out.println(路径);
}
公共静态字符串getAbsolutePath(映射文件夹映射,文件夹文件夹){
文件夹当前=文件夹;
列表缓冲区=新的ArrayList();
while(true){
整数parentId=current.getParentId();
add(current.getDesc());
if(parentId!=null){
current=folderMap.get(parentId);
}否则{
打破
}
}
收款。反向(缓冲);
返回字符串。join(“/”,buffer);
}

首先,您的代码看起来不错。它可以编译、解决问题,而且可能不会成为性能瓶颈(除非有很多文件夹)。然而,寻找改进的方法对你有好处。顺便说一句,这里有一个堆栈交换站点

为了提高一点效率,我会预先处理文件夹,这样我就不必每次都遍历整个列表来按id查找父文件夹。只需将文件夹放在
映射中(使用id作为键),这样就可以轻松快速地按id查找父文件夹


为了提高可读性,我还将用一个简单的循环替换递归。这将更容易看到,更易于调试,甚至可能更高效。

1)通过
id
将文件夹放入
Map
。2) 跳过递归。不确定你是否正确,不是瓶颈——一个小文件系统可能有1000个文件。该代码循环遍历每个父级的所有文件,因此它是
O(p*n)
。我可以看到这段代码很快就成了瓶颈。更新后的帖子,现在可以了吗?谢谢你的建议:)
public static void main(String[] args) {
        List<Folder> list = new ArrayList<Folder>();
        list.add(new Folder(1, null, "root1"));
        list.add(new Folder(3, 2, "child2"));
        list.add(new Folder(2, 1, "child1"));
        list.add(new Folder(5, 4, "child4"));
        list.add(new Folder(6, null, "root2"));
        list.add(new Folder(4, 3, "child3"));
        Map<Integer, Folder> folderMap = list.stream().collect(Collectors.toMap(
                Folder::getId, folder -> folder
        ));

        Integer fromId = 5;
        String path = getAbsolutePath(folderMap, folderMap.get(fromId));
        System.out.println(path);
    }

    public static String getAbsolutePath(Map<Integer, Folder> folderMap, Folder folder) {
        Folder current = folder;
        List<String> buffer = new ArrayList<>();
        while(true) {
            Integer parentId = current.getParentId();
            buffer.add(current.getDesc());
            if (parentId != null) {
                current = folderMap.get(parentId);
            } else {
                break;
            }
        }
        Collections.reverse(buffer);
        return String.join("/", buffer);
    }