title: How to convert list of group-by objects into html unordered list in Java?
body: <p>I have list of grouped objects into a ArrayList and I want to display it as a html unordered list into a jsp. Please see attached image for the description.<img src="https://i.stack.imgur.com/itdqK.png" alt="Click here for image"></p>
  
  <p>So far I have created a java POJO class as per below:</p>
  
  <pre><code>public class CountData {
  
      private String country;
      private String state;
      private String city;
      private String mfc;
      private int count;
  
      public CountData(String country, String state, String city, String mfc, int count) {
          this.country = country;
          this.state = state;
          this.city = city;
          this.mfc = mfc;
          this.count = count;
       }
  
       public String getCity() {
         return city;
       }
  
       public void setCity(String city) {
         this.city = city;
       }
       // Other getters ans setters
  }
  </code></pre>
  
  <p>Also created a class to store the objects into grouped list. This class is just created for testing. In actual example, its generated from group by SQL statement's return. In actual example, the data is different and more complicated so thought to explain with some simple sample data. The grouping could be in any order.</p>
  
  <pre><code>    public class CountList {
  
      private List&lt;CountData&gt; dataList;
  
      public CountList() {
          if (this.dataList == null) {
              this.dataList = new ArrayList&lt;CountData&gt;();
          }
          //Sorted Grouped List
          this.dataList.add(new CountData("China", "State3", "City3", "Female", 40));
          this.dataList.add(new CountData("India", "State1", "City1", "Female", 10));
          this.dataList.add(new CountData("India", "State1", "City1 ", "Male", 40));
          this.dataList.add(new CountData("India", "State2", "City2", "Female", 25));
          this.dataList.add(new CountData("India", "State2", "City2", "Male", 30));
          this.dataList.add(new CountData("USA", "IL", "Chicago", "Female", 20));
          this.dataList.add(new CountData("USA", "IL", "Chicago", "Male", 50));
          this.dataList.add(new CountData("USA ", "IL", "Schaumburg", "Female", 17));
          this.dataList.add(new CountData("USA", "IL", "Schaumburg", "Male", 30));
          this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Children", 10));
          this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Female", 25));
          this.dataList.add(new CountData("USA", "WI", "Milwaukee", "Male", 40));
          this.dataList.add(new CountData("USA", "WI", "Madison", "Female", 18));
          this.dataList.add(new CountData("USA", "WI", "Madison", "Male", 30));
      }
      public List&lt;CountData&gt; getDataList() {
          return dataList;
      }
      public void setDataList(List&lt;CountData&gt; dataList) {
          this.dataList = dataList;
      }
  }
  </code></pre>
  
  <p>Now I want to convert it to HTML ordered list as something like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;China (40)
          &lt;ul&gt;
              &lt;li&gt;State3 (40)
                  &lt;ul&gt;
                      &lt;li&gt;City3 (40)
                          &lt;ul&gt;
                              &lt;li&gt;Female (40)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;India (115)
          &lt;ul&gt;
              &lt;li&gt;State1 (50)
                  &lt;ul&gt;
                      &lt;li&gt;City1 (50)
                          &lt;ul&gt;
                              &lt;li&gt;Female (10)&lt;/li&gt;
                              &lt;li&gt;Male (40)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;State2 (65)
                  &lt;ul&gt;
                      &lt;li&gt;City2 (65)
                          &lt;ul&gt;
                              &lt;li&gt;Female (25)&lt;/li&gt;
                              &lt;li&gt;Male (30)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;USA (245)
          &lt;ul&gt;
              &lt;li&gt;IL (117)
                  &lt;ul&gt;
                      &lt;li&gt;Chicago (70)
                          &lt;ul&gt;
                              &lt;li&gt;Female (20)&lt;/li&gt;
                              &lt;li&gt;Male (50)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;Schaumburg (47)
                          &lt;ul&gt;
                              &lt;li&gt;Female (17)&lt;/li&gt;
                              &lt;li&gt;Male (30)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;WI (128)
                  &lt;ul&gt;
                      &lt;li&gt;Milwaukee (75)
                          &lt;ul&gt;
                              &lt;li&gt;Children (10)&lt;/li&gt;
                              &lt;li&gt;Female (25)&lt;/li&gt;
                              &lt;li&gt;Male (40)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;Madison (48)
                          &lt;ul&gt;
                              &lt;li&gt;Female (18)&lt;/li&gt;
                              &lt;li&gt;Male (30)&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>What will be the best way to achieve this. Sample code would be helpful. I am having issue in getting children and get count of each parent. In actual example, the data is different and more complicated so thought to explain with some simple sample data. The grouping could be in any order.</p>
  
  <p>Thank you.</p>