简体   繁体   English

迭代嵌套列表(任何深度)

[英]iterate over nested list (any depth)

How iterate over nested list (any depth)?如何迭代嵌套列表(任何深度)? I have the following structure我有以下结构

Com 1
    Com 2
        Com 55
            Com 551
            Com 552
        Com 56
        Com 57
            Com 66
            Com 67
                Com 99
    Com 3
        Com 33
        Com 34
        Com 35
    Com 4
        Com 41
        Com 42
            Com 421
            Com 423

I want export data to txt file with hiearchy.我想将数据导出到具有层次结构的 txt 文件。 How detect when should I add "space" to make hierarchy?如何检测何时应该添加“空格”来建立层次结构?

@Entity
public class Company {
    private Long id;
    private String name;
    private List<Company> children = new ArrayList<>();


    public Company() {
    }
    
    //getters and setters
    
    public Stream<Company> flattened() {
        return Stream.concat(
                Stream.of(this),
                children.stream().flatMap(Company::flattened));
    }
}

Assuming that you dont have cyclic company references (so no child company points to one of its parents) you can do this recursivly like this:假设您没有循环公司引用(因此没有子公司指向其父母之一),您可以像这样递归地执行此操作:

public static void print(Company company, int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print(" ");
        }
        System.out.println(company.getName());
        for (Company child : company.getChildren()) {
            print(child, depth + 1);
        }
}

Stream approach Stream 方法

@Entity
public class Company {
...
    public Stream<String> indentedNames() {
        return indentedNames(0);
    }

    private Stream<String> indentedNames(int level) {
        return Stream.concat(
                Stream.of(" ".repeat(level) + this.getName()),
                children.stream().flatMap(c -> c.indentedNames(level + 1)));
    }
...
}
    public static void main(String[] args) {
        Company company1 = new Company("1");
        Company company2 = new Company("2");
        Company company3 = new Company("3");
        Company company4 = new Company("4");
        Company company5 = new Company("5");
        Company company6 = new Company("6");
        Company company7 = new Company("7");
        company1.setChildren(List.of(company2, company3));
        company2.setChildren(List.of(company4, company5));
        company4.setChildren(List.of(company6, company7));
        company1.indentedNames().forEach(System.out::println);
    }

Make flattenedWithLevel method to generate Stream of Pairs (Company and it's depth):使用 flattenedWithLevel 方法生成 Stream 对(公司及其深度):

public Stream<Pair<Integer, Company>> flattenedWithDepth(int depth) {
    return Stream.concat(
            Stream.of(new Pair<>(depth, this)),
            children.stream().flatMap(c -> c.flattenedWithDepth(depth+1)));
} 

Then you can print all stream elements in a way you need:然后您可以按您需要的方式打印所有 stream 元素:

comp.flattenedWithDepth(1)
    .forEach(p ->
        {for (int i=0; i < p.getKey(); i++) 
            System.out.print(" ");
         System.out.println("Com " + p.getValue().getId());
        });  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM