Hierarchical Looping
<p>I have a SQL table with data as follows.</p>

<p><a href="https://i.stack.imgur.com/dDBd3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dDBd3.png" alt="enter image description here"></a></p>

<p>I have a requirement to retrieve the program-calls(Right column) for  program-names as a LIST(ArrayList) .</p>

<p>Assuming that, I have to retrieve program-calls for program-name = A, the select returns back the results(program-calls) as follows..</p>

<pre><code>B,C,D,E,F,G,H,I
</code></pre>

<p>However, I will have to select on each of the program names to see if B has any calls... in that case, B returns ...</p>

<pre><code>Z,Q,E,R,T 
</code></pre>

<p>My list should add B's data --> <code>Z,Q,E,R,T,B,C,D,E,F,G,H,I</code> . I added B's data to the final list ( Including B). I have to trickle down all the way until the and build a flat list.</p>

<p>For the program-name = A . </p>

<p>I am looking to get a result of something like this.</p>

<pre><code>A,Z,L,Q,E,R,T,B,C,D,E,F,G,H,I
</code></pre>

<p>Here is the Code that I have written until now to retrieve the data and storing it in a list.</p>

<pre><code>        String[] alTogetherString = null;
    SessionFactory sessionFactory = null;
    Session session  = null;
    HibernateUtil h1 = null;
    List&lt;String&gt; abendInfo = new ArrayList&lt;String&gt;();   
    String xmlStrings = "";
    // Configure the session factory
    h1 = new HibernateUtil();
    sessionFactory =  h1.buildSessionFactory();
    session = sessionFactory.openSession();
    session.beginTransaction();
    System.out.println("Successfully connected");

    Map&lt;String, List&lt;String&gt;&gt; map3 = null;


    map3 = new HashMap&lt;String, List&lt;String&gt;&gt;();


    Criteria crteria = session.createCriteria(Delays.class)
            .add(Restrictions.eq("programName", "A"));
    List&lt;Delays&gt; DelaysList = crteria.list();

    System.out.println(DelaysList.size());

    for(Delays d : DelaysList)
    {
        String alTogther = d.getProgramCalls();
        alTogetherString = alTogther.split(",");
    }

    // Create a list to put the first list of options...
    List&lt;String&gt; firstListOfOptions = new ArrayList&lt;String&gt;();
    for(String exd : alTogetherString)
    {
        firstListOfOptions.add(exd);
    }

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
</code></pre>

<p>Please help me through this hierarchical looping</p>