Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

diffrence between hashcode and equals method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anybody explain me the difference between hashCode() and equals() methods. I have read it from book but not quiet clear how to use them in programs. If possible please give some example also. How hashCode() comes in picture while working with == and equals() methods.


Thanks in advanced.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://en.wikipedia.org/wiki/Hashcode
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two instances have the same value, we expect them to to be equals, but they should also have the same hash value.

A hash value is like a quick-lookup version of the value of the instance. If they are equal then they must have the same hash value, but having the same hash value doesn't necessarily mean they are equal.

HashMaps use this 'quick lookup' property of Objects create fast storage. Provided the Class implementation behaves responsibly, an instance can be found in constant time, regardless of the size of the HashMap. This is pretty impressive considering there may be millions, 10's, 100's of millions or billions of objects in the hashMap.

It does this by looking for the 'hash' value, and then finding whether the retrieved instances is 'equals' as a second step. Using only equals would require looking at every one of the billion items.
 
Marshal
Posts: 74389
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Do a Google search for Joshua Bloch's Effective Java; somewhere on the Sun Java� website there is a sample of this book (chapter 3) and it has equals and hashCode in (at least it used to). Also go through the Object class API documentation where you find the specifications for hashCode and equals.
 
Geetu Kumari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks !
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic