HashMap and Vectors problem
Hi all,
I had this problem for a week, I tried to build a algrethem that store datas counting every 2, in another words, I want to group two things together, I made a testing program, using vector to store the two strings, and then put it into a HashMap, when I print out I always get below, anybody can help me....
publicclass testVector{
public testVector(){
int mod =2;
Vector test1 =new Vector();
Vector test2 =new Vector();
Vector tmp =new Vector();
HashMap hm =new HashMap();
String foo ="foo";
int count = 0;
for(int i=1; i<6; i++){
tmp.addElement(foo+new Integer(i).toString());
System.out.println("trace tmp ="+foo+new Integer(i).toString());
if(i%mod==0){
tmp.addElement(foo+new Integer(i+10).toString());
test1=tmp;
hm.put((new Integer(count)).toString(), tmp);
tmp.clear();
count++;
System.out.println("inside iffffff ");
}
}
String[] keyArr = (String[])hm.keySet().toArray(new String[hm.keySet().size()]);
System.out.println("keyArr.size="+keyArr.length);
for(int i=0; i<keyArr.length; i++){
Vector v= (Vector)hm.get(keyArr);
System.out.println("test2.v.size..."+v.size());
for(int k=0; k><v.size(); k++){
System.out.println("v.size= "+v.size());
System.out.println("test1 contain= "+v.get(k));
}
}
}
publicstaticvoid main(String[] args){
testVector t=new testVector();
}
}
and output:
====================================
trace tmp =foo1
trace tmp =foo2
inside iffffff
trace tmp =foo3
trace tmp =foo4
inside iffffff
trace tmp =foo5
keyArr.size=2
test2.v.size...1
v.size= 1
test1 contain= foo5
test2.v.size...1
v.size= 1
test1 contain= foo5
================================
I want to store "foo1, foo2" in a vector and "foo3, foo4" in a vector, it seems HashMap only stored the "foo5", Why? I need "foo1,foo2" and "foo3, foo4" to be saved into vectors.
Thanks>
# 11
Hi thanks for you reply, I tried to replace
tmp.clear()
with
tmp = new Vector();
I add two values, but I always get the last one, why?
public class testVector {
private testBean[] testBeans = null;
public testVector(){
int mod =2;
Vector test1 = new Vector();
Vector test2 = new Vector();
Vector tmp= new Vector();
HashMap hm = new HashMap();
String foo = "foo";
int count = 0;
int number = 0;
for(int i=1; i<6; i++){
tmp.addElement(foo+new Integer(i).toString());
System.out.println("trace tmp ="+foo+new Integer(i).toString());
if(i%mod==0){
number = tmp.size();
test1=tmp;
hm.put((new Integer(count)).toString(), test1);
count++;
System.out.println("inside iffffff ");
}
tmp=new Vector();
}
String[] keyArr = (String[])hm.keySet().toArray(new String[hm.keySet().size()]);
System.out.println("hashMap.sizeeeeeeeeeeeeeeeeee="+keyArr.length);
for(int i=0; i<keyArr.length; i++){
Vector v= (Vector)hm.get(keyArr[i]);
System.out.println("each vector in hm size..."+v.size());
for(int k=0; k >< v.size(); k++){
System.out.println("test1 contain= "+v.get(k));
}
}
}
public static void main(String[] args){
testVector t=new testVector();
}
}public class testVector {
private testBean[] testBeans = null;
public testVector(){
int mod =2;
Vector test1 = new Vector();
Vector test2 = new Vector();
Vector tmp= new Vector();
HashMap hm = new HashMap();
String foo = "foo";
int count = 0;
int number = 0;
for(int i=1; i<6; i++){
tmp.addElement(foo+new Integer(i).toString());
System.out.println("trace tmp ="+foo+new Integer(i).toString());
if(i%mod==0){
number = tmp.size();
test1=tmp;
hm.put((new Integer(count)).toString(), test1);
count++;
System.out.println("inside iffffff ");
}
tmp=new Vector();
}
String[] keyArr = (String[])hm.keySet().toArray(new String[hm.keySet().size()]);
System.out.println("hashMap.sizeeeeeeeeeeeeeeeeee="+keyArr.length);
for(int i=0; i<keyArr.length; i++){
Vector v= (Vector)hm.get(keyArr[i]);
System.out.println("each vector in hm size..."+v.size());
for(int k=0; k >< v.size(); k++){
System.out.println("test1 contain= "+v.get(k));
}
}
}
public static void main(String[] args){
testVector t=new testVector();
}
}
output:
============================
trace tmp =foo1
trace tmp =foo2
inside iffffff
trace tmp =foo3
trace tmp =foo4
inside iffffff
trace tmp =foo5
hashMap.sizeeeeeeeeeeeeeeeeee=2
each vector in hm size...1
test1 contain= foo2
each vector in hm size...1
test1 contain= foo4
==================================
Thanks