您好 我想用值=>填充一个hashmap 关键:idissue 价值:arraylist(受让人名单) 我的函数为我的所有问题返回同一个受理人的监听的问题: 我的地图{10700 = [x,y,z],10500 = [x,y,z],10600 = [x,y,z] .......
或者结果应该是这样的: 我的地图{10700 = [x,y,z],10500 = [w,v,q,v,t,z],10600 = [m,r,t,i] ....... 我想清除我的清单listassignee.clear();在put指令之后但在这种情况下是空的: 我的地图{10700 = [],10500 = [],10601 = [],10400 = [],10401 = [],10301 = [],10300 = [],10600 = []}
有任何建议吗?
public HashMap<String ,ArrayList<String>> MAPAssigneeByissue() throws SQLException
{
Issue iss =new Issue(); //a class return list of issue
ArrayList<String> listassignee = new ArrayList<String>();
HashMap<String ,ArrayList<String>> mapTicketassignee = new HashMap<String ,ArrayList<String>> ();
Connectionx conn=new Connectionx();
for (Iterator it = iss.getListissue().iterator();it.hasNext();)
{
Object o = (Object) it.next();
String sql2;
sql2=" my SQL:retrieve list assignee for each ticket (o.tostring) ";
ResultSet rs2 = conn.st. executeQuery(sql2);
while(rs2.next())
{
String str= rs2.getString(1);
listassignee.add(str);
}
mapTicketassignee.put(o.toString(),listassignee);
//listassignee.clear();
}
System.out.println("My map"+mapTicketassignee.toString());
return mapTicketassignee;
}
答案 0 :(得分:4)
您需要为每个地图值创建受理人列表的新实例,而不是清除列表。
基本上这行必须在你的循环内而不是之前:
ArrayList<String> listassignee = new ArrayList<String>();
答案 1 :(得分:2)
每次迭代时
listassignee = new ArrayList<String>()
答案 2 :(得分:1)
我已经重新编写代码以使其正常工作(尽管风格问题)
public HashMap<String, ArrayList<String>> MAPAssigneeByissue() throws SQLException {
Issue iss = new Issue(); //a class return list of issue
HashMap<String, ArrayList<String>> mapTicketassignee = new HashMap<String, ArrayList<String>> ();
Connectionx conn=new Connectionx();
for (Iterator it = iss.getListissue().iterator();it.hasNext();) {
Object o = (Object) it.next();
String sql2 = "my SQL:retrieve list assignee for each ticket (o.tostring) ";
ResultSet rs2 = conn.st.executeQuery(sql2);
ArrayList<String> listassignee = new ArrayList<String>();
while(rs2.next()) {
String str = rs2.getString(1);
listassignee.add(str);
}
mapTicketassignee.put(o.toString(), listassignee);
}
System.out.println("My map"+mapTicketassignee.toString());
return mapTicketassignee;
}