Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2005, 02:29 AM
Authorized User
 
Join Date: Dec 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Abt Hash Code..

Dear Friends,

            i want to know , how they are Generating Hsah Code .

what is the use of Hsahcode.....
 
Old January 16th, 2006, 05:07 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the Object class, hash codes correspond to 32-bit internal JVM address of the object in memory. In the String class, hash codes are calculated using an algorithm where two Strings with the same contents will always have the same hash code. In both cases, an effort is made to distribute the hash code values across the 32-bit spectrum. I'll explain the importance of this below.

The basic contract of any hash coding algorithm is that if two objects are to be considered equal, they must share the same hash code. Therefore if two objects have different hash codes, you know they are different. Java's built-in Collections classes often use this trick in order to speed up performance. You can also compare hashCode values in your own code.

But the primary use I have for hashCodes is the java.util.HashMap and java.util.HashSet classes. Because hashCodes are evenly distributed, these maps and sets can use the hash code to distribute objects placed in them across an array. Later, when objects need to be looked up, the hash code can be used to find the location without requiring any looping or other iterations. Such algorithms are highly regarded for their performance in larger systems -- so the HashMap is very popular due to its use of hash codes.

For a more technical explanation, you might want to read this: http://mindprod.com/jgloss/hashcode.html

Hope this helps!

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
 
Old January 18th, 2006, 03:51 PM
Authorized User
 
Join Date: Dec 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok,

  if two Strings have same hash code . then, how

they will be stored ?
 
Old January 18th, 2006, 06:31 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by pandian
if two Strings have same hash code . then, how they will be stored ?
I am assuming you're asking about how Strings are stored in a HashMap (or other hash-based storage device). If the two Strings are equal (that is, str1.equals(str2) is true), they cannot both be used as keys in a HashMap. One will override the other.

If they are not equal, then the HashMap interface will allow both to act as keys. In a traditional HashMap implementation, allowing both would be enabled through failing-over to the next available key row. Then the getter method would know that if a row is taken by something other than what's asked for, look in the next row and so on. This has some negative performance aspects to it, but in general it's pretty rare for two keys to have the same hash value.

If you're not talking about how Strings are stored with respect to HashMaps, let me know. (Basically they're stored in memory in their own space -- they just happen to have the same HashCodes because their hash code algorithm is String value based, not memory location based.)

Jon Emerson
Adobe Systems, Inc.
http://slashdot.org/~panaceaa





Similar Threads
Thread Thread Starter Forum Replies Last Post
purpose of hash in CSS style crmpicco CSS Cascading Style Sheets 4 January 27th, 2018 08:34 AM
Comparing two images in C# (without HASH) aakash494 C# 6 June 9th, 2009 11:29 AM
Dynamic load of hash table jcwest Visual Basic 2005 Basics 0 October 29th, 2007 11:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.