• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of hascode()  RSS feed

 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is te exact use of hash code?

Thanks & Regards,
Sita Raman
[ January 07, 2008: Message edited by: David O'Meara ]
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read about hashCode and about equals in the API documentation for java.lang.Object? It tells you what hashCode is needed for, there.
 
Ashok Kumar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashCode is what java uses to Store/Retrieve objects from a collection.
Its just like a RollNumber which is used to identify a student in a class.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. You are right that hash codes are used for storing and retrixval (commonly in Maps), but it is not quite like a roll number. A roll number has to be different for all different instances, but a hash code doesn't have to differ.

Sorry I have to spell retrixval wrongly because the website thinks the correct spelling is an illegal HTML tag. :roll:
 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply.

So Hashcode is used to store and retrive an object in collection(like set & map).

Its not similar to Roll number.Because if the two strings are equal the hash code must be equal(this is the logic used by set to avoid duplicate).

Am I right!

Regards,
Sitaraman
 
Brett Lounsbury
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hashCode is used to store and retrieve an object in certain collections that are backed by a hashtable in some fashion. For example, the HashMap and HashSet classes both make use of this for storage and retrieval, but the ArrayList class does not. ArrayList uses indexes for this.

Also, for example, TreeMap is an example of a Map that does not make use of hashCode (or atleast, I cant see any way for it to)... this is an instance of a Map backed by a red-black tree, and likely uses the compareTo method to walk the tree.

I'd recommend doing some research on the hashtable data structure for more information.

Cheers,
Brett
 
sita raman subra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,

regards,
sitaraman
 
Yelamuri Chandu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actual usage of hashcode is not the rollnumber or some thing else.
In the object class we have this hashcode.

In any collection which we are using the objects are stored and retreived according to the hashcode.

In Java heap the objects are maintaing in the form of buckets.
when ever you are inserting any element into to your collection, if it will look in which bucket to insert this hashcode is defined be the data in the object. if you effective manage this hashcode in you programe then the efficenty of you progarame will increase.

This hashcode will come into picture where ever you are maintianing Userdefined objects into the collections. In this case if your are not handling hashcode efficently then all your insertions will be inserting into one bucket and reduce your performce of the collection object
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct about buckets, but buckets are usually a feature of a Map rather than a Collection. You want the hashcodes (particularly the least significant bits) to differ as much as possible when elements are not equal.

If I remember correctly, HashMap uses an array presumably sorted by hashcode; each array element can represent a bucket, and the buckets are filled by adding elements to a linked list.

Most collection classes don't use buckets; ArrayList uses an array, and LinkedList uses self-referential nodes. There are a few collection classes however (eg HashSet) which has a Map which stores their elements, and those probably use buckets.

If you look in your Java folder on your PC you will find a document called src.zip; if you unzip that and look inside it you can find the code of most of the classes, including HashMap and ArrayList, so you can look and see how they are implemented.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!