Java大师,感谢您阅读我的问题。我会直言不讳。 当我循环遍历数据库ResultSet对象并使用put方法更新LinkedHashMap时,所有先前添加的值都在更新。关键是Columnname和Value是给定列的属性列表。
this.colpropmap=new LinkedHashMap<String, LinkedList<String>>();
LinkedList<String> colproplist=new LinkedList<String>();
while(crs.next()) {
colproplist.clear();
//Debug Statement
//System.out.println(crs.getString("COLNAME") + " " + crs.getInt("COLNO"));
colproplist.add(Integer.toString(crs.getInt("COLNO")));
colproplist.add(crs.getString("DATATYPE"));
//Debug Statement
System.out.println("\t" + crs.getString("COLNAME") + ": " + colproplist);
colpropmap.put(crs.getString("COLNAME"), colproplist);
System.out.println("\t" + colpropmap);
}
对于每次迭代,我看到所有以前的键都使用最新的Key值进行更新。有什么想法在这里发生了什么?
EMPID : [0, INTEGER(4,0)]
{EMPID =[0, INTEGER(4,0)]}
NAME : [1, VARCHAR(30,0)]
{EMPID =[1, VARCHAR(30,0)], NAME =[1, VARCHAR(30,0)]}
SALARY : [2, DECIMAL(5,2)]
{EMPID =[2, DECIMAL(5,2)], NAME =[2, DECIMAL(5,2)], SALARY =[2, DECIMAL(5,2)]}
EMPID : [0, INTEGER(4,0)]
{EMPID =[0, INTEGER(4,0)]}
NAME : [1, VARCHAR(25,0)]
{EMPID =[1, VARCHAR(25,0)], NAME =[1, VARCHAR(25,0)]}
SALARY : [2, DECIMAL(5,2)]
{EMPID =[2, DECIMAL(5,2)], NAME =[2, DECIMAL(5,2)], SALARY =[2, DECIMAL(5,2)]}