• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between hashcode() and equals() methods?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10259
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I learned a lot of things from you!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Guo wrote:Thanks guys, I learned a lot of things from you!


Like that you should read the documentation before posting a question?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic