• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

What does hashCode() method really mean?

 
Greenhorn
Posts: 6
Tomcat Server Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can Somebody help regarding hashCode() method of Object class.What is its importance?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you know what is hashing technique? have a look at hash based collection like java.util.HashMap
 
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
The hashCode() method returns a number that is a kind of identifier for the object, but it isn't necessarily a unique identifier.

Hash-based collection classes (such as HashMap and HashSet) use this to organize the objects that they contain in such a way that they can quickly look objects up.

For example, look at HashSet. You can imagine a HashSet as a collection of buckets. Each bucket can contain one or more objects, and each bucket has a label. Let's make it even more concrete for the example. Suppose you have a HashSet with 10 buckets. When you put an object in the HashSet, it will look at the last digit of the hash code of the object and put the object in that bucket. So, if the hash code ends with 0, it will go into bucket 0; if it ends with 1, in bucket 1, etc. By doing it that way, the HashSet distributes all the objects that you put into over its 10 buckets.

Now, suppose that you want to check if an object is present in the HashSet. You call contains(someObject) on the HashSet. The HashSet now looks at the hash code of someObject, and immediately knows in which of the 10 buckets to look. It can skip the other 9 buckets, which saves a lot of work. It only has to call equals() on the objects that are in one of the buckets.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic