aspose file tools*
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
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: 19723
    
  20

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: 1509
    
    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: 14274
    
  21

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 8 API documentation
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

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: 14274
    
  21

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the difference between hashcode() and equals() methods?