我有以下数据库结构
mysql> select * from categories limit 30;
+-------------+-------------+-----------------+-------+------+------+------+------+------+------+------+
| category_id | T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10 |
+-------------+-------------+-----------------+-------+------+------+------+------+------+------+------+
| 1 | Popcorn | Regular Pack | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 1 | Popcorn | Regular Pack | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 1 | Popcorn | Regular Pack | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | Popcorn | Bucket | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | Popcorn | Bucket | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2 | Popcorn | Bucket | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | Popcorn | Jumbo | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | Popcorn | Jumbo | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 4 | Popcorn | Tub | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 4 | Popcorn | Tub | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 5 | Popcorn | Combo Cool Bite | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 6 | Popcorn | Combo Relish | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 7 | Soft Drinks | Fountain | Apple | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 8 | Soft Drinks | Fountain | Orange | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 8 | Soft Drinks | Fountain | Orange | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 9 | Soft Drinks | Fountain | Lemon | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 9 | Soft Drinks | Fountain | Lemon | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 10 | Soft Drinks | Fountain | Clear | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 10 | Soft Drinks | Fountain | Clear | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 11 | Soft Drinks | Tin | Apple | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 11 | Soft Drinks | Tin | Apple | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 12 | Soft Drinks | Tin | Lean Apple | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 12 | Soft Drinks | Tin | Lean Apple | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+-------------+-------------+-----------------+------------+------+------+------+------+------+------+------+
我正试图以这种格式为上面的内容创建一个Json结构。
例如
以下两个数组。 (伪代码)
PopCorn :[Regular Pack, Bucket ,Jumbo,Tub,Combo Cool Bite,Combo Relish]
Soft Drinks:[Fountain,Tin]
我试图获得T1的所有不同元素并组合T2的不同元素
我能够以这种方式做到:
JSONArray T1array=new JSONArray();
JSONArray T2array=new JSONArray();
ResultSet rs=stmt.executeQuery();
while(rs.next())
{
String T1 = rs.getString("T1");
String T2 = rs.getString("T2");
if(!tempvariable.equals(T1))
{
T1array.put(T1);
// T2array.put(T2);
}
tempvariable = T1;
}
请告诉我如何将T2映射到T1
答案 0 :(得分:2)
可能的解决方案可能是
Map<String, Set<String>> tempMap = new HashMap<String, Set<String>>();
ResultSet rs=stmt.executeQuery();
while(rs.next())
{
String T1 = rs.getString("T1");
String T2 = rs.getString("T2");
Set<String> t2 = tempMap.get(T1);
if(t2 == null)
{
t2 = new HashSet<String>();
tempMap.put(T1, t2);
}
t2.add(T2);
}
之后,您在tempMap中将所有不同的T1作为键,并在Set值中为它们设置所有不同的T2。