2 Replies Latest reply: Sep 14, 2012 4:56 AM by Kayaman RSS

    Grouping using Collections framework

    770386
      Hi Friends

      I have a requirement where I have a table with columns Parent, Children, GrandChildren, GreatGrandChildren, E, F and G and I want to group them not using sql but using the collections framework. Like value in column Parent can have multiple instances of Children, similarly value in Children can have multiple instances of GrandChildren and value in GrandChildren can have multiple instances of GreatGrandChildren.

      Here the data in the table is not already grouped in the order in which it is to be retrieved and has values in some ad hoc order. Would appreciate if anyone can help me with an example

      Thanks
        • 1. Re: Grouping using Collections framework
          961708
          This seems, your going much deep down if you simply see the problem, recursive approach to design your class can do your trick.

          Imagine everything is just an Element, either a parent or a child, or a grand child what ever it be.
          public class Element {
             private Element element;                    // This indicates an element at n level. 
             private List<Elelement> children;         // This will have all of its immediate children.  
          
             private Element parent;                      //This will help you to point immediate parent of an element. 
          
          }
          If you tell your use case, I probably can help to give u specific example to group them by using Java collection API.
          • 2. Re: Grouping using Collections framework
            Kayaman
            user13019661 wrote:
            I want to group them not using sql but using the collections framework.
            Why? And why can't you do both?