1
\$\begingroup\$

I am trying to work with a list of Person objects where the Person object has a property called "children" which is an array of same type. I idea is to have methods called “addChildrenToMain” and “removeChildrenFromMain”. In case of addChildrenToMain I want to take the child objects and insert into the array just of the parent. For removeChildrenFromMain, find out number of children (plus children’s children if expanded) and remove those many elements from the list.

I tried to simplify the problem for clarity, and this is what I have. Some things about the problem.

  • The List wouldn’t be very large
  • We should remove children from main only when its expanded.
  • Max limit of the children levels is 10

I specifically would like to know if the underlying approach can result in accidental deletion/addition during adding/removing (in the resulting array).

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

public class PersonList {
    public static void main(String[] args) {
        List<Person> persons = getPersons();
        System.out.printf("----------- ORGINAL ----------\n");
        persons.forEach(System.out::println);
        persons = addChildrenToMain(persons, 0);
        System.out.printf("----------- EXPAND Emma ----------\n");
        persons.forEach(System.out::println);
        //collapse(persons, 0);
        persons = addChildrenToMain(persons, 2);
        System.out.printf("------------EXPAND Emma Jr2-----------\n");
        persons.forEach(System.out::println);
        //collapse(persons, 0);
        removeChildrenFromMain(persons, 2);
        System.out.printf("------------COLLAPSE -----------\n");
        persons.forEach(System.out::println);
    }

    private static List<Person> addChildrenToMain(List<Person> persons, int i) {
        List<Person> children = persons.get(i).children;
        if (children != null && children.size() > 0 && !persons.get(i).isNodeExpanded) {
            persons.get(i).isNodeExpanded = true;
            persons.addAll(i + 1, children);
        }
        return persons;
    }

    private static List<Person> removeChildrenFromMain(List<Person> persons, int i) {
        List<Person> children = persons.get(i).children;
        if (children != null && children.size() > 0 && persons.get(i).isNodeExpanded) {
            //Need to find out how many elements that need to be deleted
            //starting i+1 index
            int numElementsToDelete = getExpandedChildCount(persons.get(i));
            System.out.println("Number of children for " + persons.get(i).name + " = " + numElementsToDelete);
            if (numElementsToDelete > 0) {
                persons.subList(i + 1, i + 1 + numElementsToDelete).clear();
                persons.get(i).isNodeExpanded = false;

            }
        }
        return persons;
    }

    private static int getExpandedChildCount(Person person) {
        int count = 0;
        if (person.children != null && person.children.size() > 0 && person.isNodeExpanded) {
            count = count + person.children.size();
            for (Person p : person.children) {
                count = count + getExpandedChildCount(p);
            }
        }
        return count;
    }


    private static List<Person> getPersons() {
        List<Person> persons = new ArrayList<>();
        Person emma = new Person("Emma");
        Person emmaChild1 = new Person("Emma Jr1");
        Person emmaChild2 = new Person("Emma Jr2");

        Person emmaGrandChild1 = new Person("Emma Grand Child1");
        Person emmaGrandChild2 = new Person("Emma Grand Child2");
        List<Person> emmaGrandChild = new ArrayList<>();
        emmaGrandChild.add(emmaGrandChild1);
        emmaGrandChild.add(emmaGrandChild2);
        emmaChild2.children = emmaGrandChild;

        List<Person> emmaChildren = new ArrayList<>();
        emmaChildren.add(emmaChild1);
        emmaChildren.add(emmaChild2);
        emma.children = emmaChildren;

        //------------------------------------------
        Person olivia = new Person("Olivia");
        Person isabella = new Person("Isabella");


        persons.add(emma);
        persons.add(olivia);
        persons.add(isabella);
        return persons;
    }

}

class Person {
    public String name;
    public List<Person> children;
    public boolean isNodeExpanded;

    public Person(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return
                "NAME='" + name + '\'' +
                        ", NUM_CHILDREN=" + (children == null ? 0 : children.size()) +
                        (children == null ? "" : ", IS_EXPANDED=" + isNodeExpanded);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.