Loop Through Self List

NoobLord1234 :

I have this Person class which has a list of Person (s). How do I loop through persons and check if each object inside of that has a list of Person(s) and if each object inside that has a list and so on and so forth? Everything I can think of is pretty limiting as far as how nested it gets. I can write a recursive loop but that gets me to the first level deep, but not sure how to get x levels deep with recursion. I am sure somebody has come accross this problem in the past and it shouldn't be that difficult but I just can't quite wrap my head around it. Any and all ideas are welcomed!

public class Person {
    // other fields removed for simplicity
    private long id;
    private List<Person> persons;

    public List<Person> getPersons() {
        return debates;
    }
}

// essentially I am looking for a way to make this unlimited level nested looping
private void loopPersons() {
    Person person = new Person();

    if(person.getPersons() != null && !person.getPersons().isEmpty()) {
        for(Person person1 : person.getPersons()) {
            if(person1.getPersons() != null && !person1.getPersons().isEmpty()) {
                System.out.println(person1.getId());

                for(Person person2 : person1.getPersons()) {
                    if(person2.getPersons() != null && !person2.getPersons().isEmpty()) {
                        System.out.println(person2.getId());
                    }
                }
            }
        }
    }
}

UPDATE: The answer by Brian in this other post (scroll down) is essentially what does it. iterate through recursive objects

Arvind Kumar Avinash :

Do it as follows:

import java.util.List;

class Person {
    // other fields removed for simplicity
    private long id;
    private List<Person> persons;

    public Person(long id, List<Person> persons) {
        this.id = id;
        this.persons = persons;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    public List<Person> getPersons() {
        return persons;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", persons=" + persons + "]";
    }

    public void showAll() {
        if (getPersons() == null || getPersons().isEmpty()) {
            return;
        }
        getPersons().get(0).showAll();
        System.out.println(getPersons());
    }
}

Demo:

public class Main {
    public static void main(String[] args) {
        Person p1 = new Person(1,List.of(new Person(11, List.of(new Person(111, List.of(new Person(1111, null))),
                                                                new Person(112, List.of(new Person(1121, null))),
                                                                new Person(113, List.of(new Person(1131, null))))),
                                        new Person(12, List.of(new Person(121, List.of(new Person(1211, null))))),
                                        new Person(13, List.of(new Person(131, List.of(new Person(1311, null)))))));
        p1.showAll();
    }
}

Output:

[Person [id=1111, persons=null]]
[Person [id=111, persons=[Person [id=1111, persons=null]]], Person [id=112, persons=[Person [id=1121, persons=null]]], Person [id=113, persons=[Person [id=1131, persons=null]]]]
[Person [id=11, persons=[Person [id=111, persons=[Person [id=1111, persons=null]]], Person [id=112, persons=[Person [id=1121, persons=null]]], Person [id=113, persons=[Person [id=1131, persons=null]]]]], Person [id=12, persons=[Person [id=121, persons=[Person [id=1211, persons=null]]]]], Person [id=13, persons=[Person [id=131, persons=[Person [id=1311, persons=null]]]]]]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355375&siteId=1