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>

[3518 byte] By [bronze-starDukes] at [2007-11-26 12:09:47]
# 1
HiYour code is reusnig the same Vector for all objects. You can't do that. You are just storing the same vector at different locations in the Map, and you are calling clear on the same instance.
platinumsta at 2007-7-7 13:46:56 > top of Java-index,Archived Forums,Socket Programming...
# 2
Thanks for the reply!Do you know how to sovled problems? Any sugestion?
bronzestar at 2007-7-7 13:46:56 > top of Java-index,Archived Forums,Socket Programming...
# 3
Use a separate Vector each time. Don't reuse the same one. Also, don't clear it after you add it to the map.
silverstar at 2007-7-7 13:46:56 > top of Java-index,Archived Forums,Socket Programming...
# 4
I created different one: test1, test2, and tmp. Are thoes incorrct? How to do that? I want to store each two into HashMap.Thannnnnnnks
bronzestar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 5
You might be creating some other vectors but you keep putting tmp in each value of the map:hm.put((new Integer(count)).toString(), tmp);
silverstar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 6
Each time I put new value into tmptmp.addElement(foo+new Integer(i+10).toString());then I clear use another looptmp.clear();
bronzestar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 7

> Each time I put new value into tmp

> tmp.addElement(foo+new Integer(i+10).toString());

> then I clear use another loop

> tmp.clear();

Yes, don't you realize that you are clearing the same list you just added to the map. So when you say tmp.clear() you are clearing the list you just added to the map?

I think you need to read up on java references to objects.

silverstar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 8
So can you give me advice how to store every two objects into a Collection(HashMap)?
bronzestar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 9
> So can you give me advice how to store every two> objects into a Collection(HashMap)?My advice would be for you to research some of the basics on how Java works, and what a reference to an object is.
silverstar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 10
Do this:tmp = new Vector();instead of tmp.clear();Kaj
platinumsta at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 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

bronzestar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 12
You changed your code and now you keep adding test1 to each valuehm.put((new Integer(count)).toString(), test1);
silverstar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 13
I have test1=tmp;
bronzestar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 14
> I have > > test1=tmp;> Why are you doing that? What is the point?Also, I only see where you are adding one element before you create a new vector.Again, I think you need to read up on some basics.
silverstar at 2007-7-7 13:46:57 > top of Java-index,Archived Forums,Socket Programming...
# 15
Thanks zadok, I sovled problem, Thanks again!
bronzestar at 2007-7-7 13:46:58 > top of Java-index,Archived Forums,Socket Programming...