How can I get all the children of a category?

0

I have the next class

public class AllCategory {

    int id;
    String nombre; 
    int padre;
    List<Clase> hijos;

}

I receive a list of the following object:

public class Category {

    int id;
    String nombre; 
    int padre;
    List<Clase> hijos;

}

Each Category has an id, the name and the id of its parent category. I want to get an AllCategory list so that each node in the list is the father and the sub nodes are its children, I really do not know how to do it. I appreciate your help.

    
asked by Developer_a_melo 15.07.2017 в 01:38
source

1 answer

0

The best thing is that you manage a single class

public class Category {

   int id;
   String nombre; 
   Integer padre; // Si el valor es nulo, entonces es padre
   List<Category> hijos;

}

With this class then if the father is null it means that he is the father and that he has no predecessor. Otherwise all the categories within children are your children and you are keeping the father's relationship within the parent member. Check that the data type was changed to Integer to allow null to that member, the primitives (int, double, etc) do not support nulls.

    
answered by 15.07.2017 в 17:38