• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

equals and hashCode  RSS feed

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very basic (and perhaps a silly) question.
When is the equal() or hashCode() method called?
I normally dont see it being called explicitly so at what point of time is it called.Are they called one after or another or what..?
For eg.in this code.
Map object is created
Dog constructor is run and further..values and keys are inserted and later retrived.
So ...
What's the use of implementing equals and hashCode methods.
Still struggling to make the concept clear.. And having problem with it
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, when you "put" an element in a collection backed by hashing, hashCode is called behind the scenes to determine which "hashCode bucket" to store that element in. If the collection is a Set (rejecting duplicates), then equals would also need to be called to see if the element is already there.

Similarly, when you "get" an element, hashCode is called to find the correct bucket, then equals is called to find the desired element in that bucket.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
That was helpful.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little confused here.

Can some one tell me which two names are being compared in the equals() method.


ie


(((Dog)o).name==name)

Here there is only one instance variable, so wouldnt it always be the same.

Also which object is being passed to the equals(Object o).

This is so confusing...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals method compares this instance to some other object.

Suppose you have 2 references to Dogs, d1 and d2. You're going to use one of these references to call the equals method, and you're going to use the other reference as an argument to that method. For example...

d1.equals(d2)

So if the implementation of equals uses (((Dog)o).name==name), then 'o' references the object passed as an argument (the Dog referenced by d2). This reference is type Object, so it's first cast to type Dog...

(Dog)o

...then we get the name...

((Dog)o).name

...and next we compare the name of that other object (the Dog referenced by d2) to the name of the Dog on which we're calling the method (the Dog referenced by d1). Here, "name" is implicitly "this.name."

(((Dog)o).name == name)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, when comparing object references for "equality," you usually don't want to use a simple value comparison of ==, because this just tells you whether the references are pointing to the same object.

Instead, you will probably want to use the equals method on "name"...

(((Dog)o).name).equals(name)
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..

I am some what clear with the way equals() works separately but I cannot understand it with respect to this program,
rather when it is being called implicilty through some other method (from behind the curtain).

For eg as you said get(....) calls hashCode() and equals() to retrive an element..

System.out.println(m.get("k1"));

When this line is called what object is passed to the equals method and which object is it being compared to...
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc Weber kindly pay attention,

One thing i still cant follow. When we execute the commant m.put("k1",new Dog("Clover")), it is amply clear that the hashchode for String "k1" is calculated and the right bucket is found, and Clover is stuffed there. So far so good. Remember here we did not make use of the equals method as it is a Map and concept of duplicacy does not arise as in Maps the key has to be unique.

On retrieval, hashchode value of the supplied key is calculated, exact bucket is found and the object would be retreived still without using the equals method as the key to every object is uniqure.

So why to override the equals method while using hashmap ?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of now things seems to be clear, but my issue is, i have seen programs where
put(arg1,arg2)
arg1 is used as a key and arg2 as value and, vise-versa. is it possible to use
arg2 as key and arg1 as value??? if so, while we retrieve the value from buckets will not there be any prob???
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!