Hashmap Troubles

Hi All,

I'm having trouble with a HashMap in my program. It is replacing all values when a key value pair is inserted. My HashMap is declared as HashMap<Integer,ArrayList><WrapperObject>>. . When I print out the values of the HashMap the following behavior is observed:

This pair is added: {4=[Wrapper Object ID: 5], 5=[]}

This is the full hashmap: {4=[Wrapper Object ID: 5], 3=[], 5=[]}

This pair is added : {6=[Wrapper Object ID: 7], 7=[]}

This is the full hashmap:{4=[Wrapper Object ID: 7], 6=[Job Wrapper ID: 7], 3=[], 7=[], 5=[]}

Does anybody have any idea why this is occuring?

TIA,

Adam

[666 byte] By [chester122a] at [2007-11-26 17:08:07]
# 1
Doubt it has anything to do with hashmap. Check how you are creating your Wrapper Object and how you are adding them.
zadoka at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 2
Thanks for the quick reply Zadok,The WrapperObjects are not unique - the object has some internal variables updated in another method, and is then passed to the class containing the HashMap for storage. Could this be the issue? Adam
chester122a at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 3

> Thanks for the quick reply Zadok,

> The WrapperObjects are not unique - the object has

> some internal variables updated in another method,

> and is then passed to the class containing the

> HashMap for storage. Could this be the issue?

> Adam

Adding it to the hasmap doesn't make a copy of it. So if you add it to the map. Then change a variable and add it again. Then both keys point to the same object which was updated.

zadoka at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 4
Then how might I get around this issue? Could I overide hashCode? How should I appropriately copy/create a new object with the internal variables?
chester122a at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 5

> Then how might I get around this issue? Could I

> overide hashCode?

The Key is integer, so I don't think hashcode is ever called on your value object (Wrapper).

> How should I appropriately

> copy/create a new object with the internal variables?

You might want two keys pointing to the same object (which is what you have now). If you want more than one object in the map you have to do something other than add the same object again and again.

How to create a new one is up to you. (Why can't you do it the same way you created the first object?)

zadoka at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 6
I create a new object by inserting the values from the old object, but the problem is still occurring, any further ideas?
chester122a at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 7
> I create a new object by inserting the values from> the old object, but the problem is still occurring,> any further ideas?Without seeing the code I can only guess that you are still adding the same object twice to the map.
zadoka at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...
# 8

> I create a new object by inserting the values from

> the old object, but the problem is still occurring,

> any further ideas?

I don't see the code either but it's almost certain that you are just changing an existing object. Another way people find to screw this up is to declare the variables in their class static, thus making all objects of the class look alike. But as I and zadok suggested, you're the only one that can see the code.

DrClapa at 2007-7-8 23:35:57 > top of Java-index,Core,Core APIs...