This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What's the difference between hashcode() and equals() methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's the difference between hashcode() and equals() methods?

Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
What's the difference between hashcode() and equals() methods?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

One is called hashCode, the other equals.
One doesn't take any arguments, the other a single Object argument.
One returns an int, the other a boolean.

Should I go on?

What I'm trying to point out is that they are very different methods. They are linked though because of how they are used in hash maps (among others). These first use the hash code to find a bucket for a key, then use equals inside that bucket. If they aren't linked properly (as specified in the Javadoc page of Object) then this won't work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi Mark,

Perhaps this might help you.

But as Rob has mentioned, you must understand that these two are totally different methods with absolutely different purpose. Any Java book would explain it.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

These methods are both defined by class Object, so you can lookup the API documentation of them in class java.lang.Object, which explains exactly what these two methods mean and do.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Mark Guo wrote:What's the difference between hashcode() and equals() methods?

Literally, what the others have said is absolutely right; and I'd also suggest that you look at the API docs for Object.

Logically, they should be as similar as possible, given any other constraints you might have, such as time. That is to say that equal hashcodes should strongly indicate equal objects; however, a near-perfect hashCode() method won't be much use if it takes 10 seconds to run.

In addition to the other suggestions, Effective Java has a very good chapter on hashcodes in general, and the requirements of equals() and hashCode() as used together.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

hashcode() - Returns a hash code value for the object.
equals() - Indicates whether some other object is "equal to" this one.

The combined functionality of these two will we seen if we consider the example of hash code based collections, in that case while looking searching for the data in the collection firstly hashcode() will be called, which will narrow the scope to find that element and then it will call equals() method which will further help in finding that particular element in that scope.

Lets take a simple example - Say there are 5 buckets with many elements and the element you want to find has the probability to be in any of the bucket, then in that case your hascode() method will tell you the bucket and then it will call the equals() method to find your requested element in that bucket.

Hope you got some idea.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note: it should be hashCode with a capital C.

It's important to be precise about these things. If you would implement a hashcode() with lower-case c method in your class, it is not overriding the hashCode() method from class Object and your program is going to have a bug - your hashcode() method is never going to be called by hash-based collections such as HashMap and HashSet.

Java has something to prevent this bug: the @Override annotation. If you use this and you make the mistake mentioned above, the compiler will give you an error.
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Thanks guys, I learned a lot of things from you!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mark Guo wrote:Thanks guys, I learned a lot of things from you!


Like that you should read the documentation before posting a question?
 
wood burning stoves
 
subject: What's the difference between hashcode() and equals() methods?
 
Similar Threads
diffrence between hashcode and equals method
Difference between hashCode() and equals()
Difference between Hashcode and Equals() method?
What is hashCode?
equals() and hashCode()