wood burning stoves*
The moose likes Beginning Java and the fly likes Behaviour of Objects & HashMap behind the scene. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Behaviour of Objects & HashMap behind the scene." Watch "Behaviour of Objects & HashMap behind the scene." New topic
Author

Behaviour of Objects & HashMap behind the scene.

Arvind Choudhary
Greenhorn

Joined: Jun 07, 2012
Posts: 15
Hi,
It is just a scenario of behind the scene behaviour of Objects & HashMap on which I want to know your views,that's why this post. Below is my scenario -
Please let me know in case of any clearification or doubt on this.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Please let me know in case of any clearification or doubt on this.


In the future, rather than just saying, "Explain the behavior," you should indicate exactly what that behavior is, so that people don't have to guess or run your code to know what you're talking about. It's easy enough to spot this time, but please keep it in mind for future questions.

As for your question...

You should not use mutable objects for Map keys, or in Sets. The enclosing data structure does not know that the object's state has changed, and so may not be able to find it now, as it may be stored in the wrong place. If you're going to use mutable objects, then you have to remove and re-add them when they change.

In particular, there is exactly one A object in your pgoram, and when you changed its contents, you probably changed its hashCode() value and almost certainly changed what it is and is not equals() to. There's an object that was initially stuck into the "ABC" bucket, but now it belongs in the "PQR" bucket (which is probably a different bucket). So anything that's looking for that object will probably look in the wrong bucket, and won't find it.
Arvind Choudhary
Greenhorn

Joined: Jun 07, 2012
Posts: 15
Hi Jeff,

Thanks for your reply,explanation & comments(which I will take care henceforth).
My method implementation are as under -


Below is the Op that I am getting out this program -

System.out.println(" 1. Value is " + hm.get("ABC")); ----> OP - 1. Value is null
System.out.println(" 2. Value is " + hm.get(new A("ABC"))); ----> OP - 2. Value is null
System.out.println(" 3. Value is " + hm.get(new A("PQR"))); ----> OP - 2. Value is A@1

Printing the HashMap prints - {A@1=A@1}

In the above case Op of 1 &2 looks ok to me if you could please explain the value printed by 3rd SOP i.e. System.out.println(" 3. Value is " + hm.get(new A("PQR"))); ----> OP - 2. Value is A@1 .

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Your hashCode, while legal, is horrible. It completely defeats the purpose of using a hash-based data structure, since every lookup will be a linear search through all the elements, owing to the fact that there's a single bucket.

Because there's a single bucket, changing the key doesn't prevent your object from being found. "PQR" goes into the same bucket as "ABC"--the only bucket there is here. Once the bucket is determined, it's a linear search using equals, and since the object you're searching for and the one in the map are equal, it finds it and prints it out.
Arvind Choudhary
Greenhorn

Joined: Jun 07, 2012
Posts: 15
I have intentionaly kept hashCode implementational like this. Here the point is changing value of str from "ABC" to "PQR" for object a is not reflected into the HashMap as it prints the value as {A@1=A@1} then there is conflict in the behaviour of printing values in line 2 & 3. i.e.
System.out.println(" 2. Value is " + hm.get(new A("ABC"))); ----> OP - 2. Value is null
System.out.println(" 3. Value is " + hm.get(new A("PQR"))); ----> OP - 3. Value is A@1

while for 2 it is null but for 3 it is returing A@1. In my openion if value printed in sop 3 is A@1 then sop2 should also print A@1 only ??

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37878
    
  22
There is no conflict. You have managed to put a null into your Map.
Arvind Choudhary
Greenhorn

Joined: Jun 07, 2012
Posts: 15
Thats fine. Thanks all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Behaviour of Objects & HashMap behind the scene.
 
Similar Threads
variable initialisation
hashtable working & overriding equals
Searching in a Map
Doubt related to HashMap
How to refresh a StringBuffer object for creating a HashMap object