gpt4 book ai didi

java - 如何在Java中将group-by对象列表转换为html无序列表?

转载 作者:行者123 更新时间:2023-12-01 04:34:19 25 4
gpt4 key购买 nike

我将分组对象列表放入 ArrayList 中,我想将其作为 html 无序列表显示到 jsp 中。请参阅附图的描述。 Click here for image

到目前为止,我已经创建了一个 java POJO 类,如下所示:

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
}

还创建了一个类来将对象存储到分组列表中。这个类只是为了测试而创建的。在实际示例中,它是根据 SQL 语句的返回结果生成的。在实际示例中,数据不同且更复杂,因此考虑用一些简单的示例数据来解释。分组可以按任何顺序。

    public class CountList {

private List<CountData> dataList;

public CountList() {
if (this.dataList == null) {
this.dataList = new ArrayList<CountData>();
}
//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<CountData> getDataList() {
return dataList;
}
public void setDataList(List<CountData> dataList) {
this.dataList = dataList;
}
}

现在我想将其转换为 HTML 有序列表,如下所示:

<ul>
<li>China (40)
<ul>
<li>State3 (40)
<ul>
<li>City3 (40)
<ul>
<li>Female (40)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>India (115)
<ul>
<li>State1 (50)
<ul>
<li>City1 (50)
<ul>
<li>Female (10)</li>
<li>Male (40)</li>
</ul>
</li>
</ul>
</li>
<li>State2 (65)
<ul>
<li>City2 (65)
<ul>
<li>Female (25)</li>
<li>Male (30)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>USA (245)
<ul>
<li>IL (117)
<ul>
<li>Chicago (70)
<ul>
<li>Female (20)</li>
<li>Male (50)</li>
</ul>
</li>
<li>Schaumburg (47)
<ul>
<li>Female (17)</li>
<li>Male (30)</li>
</ul>
</li>
</ul>
</li>
<li>WI (128)
<ul>
<li>Milwaukee (75)
<ul>
<li>Children (10)</li>
<li>Female (25)</li>
<li>Male (40)</li>
</ul>
</li>
<li>Madison (48)
<ul>
<li>Female (18)</li>
<li>Male (30)</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

实现这一目标的最佳方法是什么?示例代码会很有帮助。我在养 child 和统计每个 parent 方面遇到问题。在实际示例中,数据不同且更复杂,因此考虑用一些简单的示例数据来解释。分组可以按任何顺序。

谢谢。

最佳答案

public String convertToHTML(ArrayList<CountData> stuffs){
StringBuilder sb = new StringBuilder()
sb.append(“<ul>\n”);
for(CountData cd : stuffs){
sb.append(“<li>”+cd.getCountry()+” (“+cd.getCount()+”)\n”);
sb.append(“\t<ul>);
sb.append(“\t\t<li>”+cd.getState()+” (“+cd.getCount()+”)\n”);
sb.append(“\t\t\t<ul>);
sb.append(“\t\t\t<li>”+cd.getCity()+” (“+cd.getCount()+”)\n”);
sb.append(“\t\t\t\t<ul>);
sb.append(“\t\t\t\t<li>”+cd.getMfc()+” (“+cd.getCount()+”)</li>\n”);
sb.append(“\t\t\t\t</ul>);
sb.append(“\t\t\t</li>);
sb.append(“\t\t</ul>);
sb.append(“\t</li>);
sb.append(“</ul>);
sb.append(“</li>);
sb.append(“\n”);
}
return sb.toString()
}

关于java - 如何在Java中将group-by对象列表转换为html无序列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17554241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com