Map<String, Object> item;
List<Map<String, Object>> list;
item = new HashMap<String, Object>;
item.put("title","item1");
item.put("price","100");
list.add(item);
item = new HashMap<String, Object>;
item.put("title","item2");
item.put("price","200");
list.add(item);
item = new HashMap<String, Object>;
item.put("title","item1");
item.put("price","300");
list.add(item);
首先,想像有一個表格(Table)如下:
title price
item1 100
item2 200
item3 300
那么,如果将上述title内容,显示到画面List列表中呢?这相当于有一個 xml 字串如下:
"<list>
<item>
<title>item1</title>
<price>100</pirce>
</item>
<item>
<title>item1</title>
<price>200</pirce>
</item>
<item>
<title>item1</title>
<price>300</pirce>
</item>
</list>"
List<Map<String, Object>> list;
item = new HashMap<String, Object>;
item.put("title","item1");
item.put("price","100");
list.add(item);
item = new HashMap<String, Object>;
item.put("title","item2");
item.put("price","200");
list.add(item);
item = new HashMap<String, Object>;
item.put("title","item1");
item.put("price","300");
list.add(item);
首先,想像有一個表格(Table)如下:
title price
item1 100
item2 200
item3 300
那么,如果将上述title内容,显示到画面List列表中呢?这相当于有一個 xml 字串如下:
"<list>
<item>
<title>item1</title>
<price>100</pirce>
</item>
<item>
<title>item1</title>
<price>200</pirce>
</item>
<item>
<title>item1</title>
<price>300</pirce>
</item>
</list>"
本文介绍了一个使用Java HashMap和List进行数据存储与展示的例子。通过创建包含title和price字段的Map对象,并将其添加到List中,最终以XML字符串形式展现出来。此例有助于理解Java集合的使用方法。

被折叠的 条评论
为什么被折叠?



