Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the difference between hashcode() and equals() methods?
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I learned a lot of things from you!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic