This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing Maps hashcodes problem??

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Dog{
public Dog(String n){
name=n;
}
public String name;
public boolean equals(Object o){
if(((Dog)o).name==name){ //THIS IS COMPARING HASHCODES NOT THE STRINGS?
return true;
}else{
return false;
}
}
public int hashCode(){
return name.length();
}
}
class MapTest{
public static void main(String a[]){
Map<Object,Object> m=new HashMap<Object,Object>();
Dog d1=new Dog("dfull");
m.put(d1,"dog one");
System.out.println(m.get(d1));
d1.name="dnull"; //IF WE CHANGE THE STRING WHICH IS NOT THE SAME STRING LENGTH THEN IT WORKS?
System.out.println(m.get(d1));
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manny.

For future reference please use the "[CODE]" tags to post code because it's very hard to read your code. Also please state the question before and/or after the code and not *in* the code.

It looks like you have 2 questions, but before I get into those let me give you a slight overview on what it looks like you're trying to do.

You have a Dog class which implements the "equals" and the "hashCode" methods. As you know when you use a hashMap or any other hash based collection the objects you "put" in the collection are placed in "buckets" that are keyed on the objects hashCode. That hashCode is then used to quickly locate the "bucket" and then search for those objects from the collection and it is important to use a unique hashCode that will uniquely identify the object you're looking for.

What i found strange about your example code is that you're only inserting one object (dog) into your hashMap, had you tried inserting more than that you would have noticed how bad Dog's implementation of hashCode and equals were. So if a bucket contains more than one object, say you had two dog objects that had the same name for example, then the equals method is used to find the object you are searching for in that bucket.

The strings "==" operator just maps to it's "equals" method which doesn't rely on the hashCode method to verify equality.

hope that helps.
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1.THIS IS COMPARING HASHCODES NOT THE STRINGS WHY?
2.WHY IT IS TRUE? (IS e==d)
3.WHY IT IS GIVING NULL?
4.WHY IT IS TRUE?
5.WHY IT IS FALSE HERE (NOW WHAT HAPPENED? IN 2 ITS FINE)
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi noam,
thankyou for your reply but i doesn't agree on your answer or if i am wrong tell me clearly please..
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manny,

The == operator compares the reference held in the variable - so the equals() implementation at //1 will return true if the name instance variable in both objects refers to the same object on the heap/in the String constant pool.

As for the other questions, the JavaDoc for Maps says that the behaviour of a Map is undefined if you change the key object whilst it is a key in the Map - so I think anything could be happening!

Quote from javadocs:

Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map



Regards,

Paul
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your answer.
but I have already used equals method..there is no change using it either.
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, as mentioned, changing the keys can corrupt your hashmap. So, nothing is guaranteed to work correctly. But here are the answers to your question.

2.WHY IT IS TRUE? (IS e==d)

It's true because d1 == d1. The two Dog objects are the same.

3.WHY IT IS GIVING NULL?

It's null because you corrupted the hashmap. The d1 object is in the previous bucket, not the correct bucket for "ddd".

4.WHY IT IS TRUE?

It's true, because you "fixed" the corruption. And because of the string pool, the two references are equal (refers to the same string object)

5.WHY IT IS FALSE HERE (NOW WHAT HAPPENED? IN 2 ITS FINE)

In 2, it's the same Dog object, which is not true here. In 4, you had the string pool to make the make the string object the same object, which is not true here either.

Henry
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much Henry.
 
reply
    Bookmark Topic Watch Topic
  • New Topic