|
|
| Line 1: |
Line 1: |
| {{DISPLAYTITLE:Java <tt>hashCode()</tt>}}
| | Hi there, I am Alyson Pomerleau and I think it seems quite good when you say it. He is an information officer. Ohio is where his home is and his family members enjoys it. I am truly fond of to go to karaoke but I've been taking on new issues lately.<br><br>Have a look at my homepage :: psychic phone - [http://www.aseandate.com/index.php?m=member_profile&p=profile&id=13352970 see post] - |
| In the [[Java (programming language)|Java]] [[programming language]], every [[class (computer science)|class]] implicitly or explicitly provides a '''<code>hashCode()</code>''' [[method (computer science)|method]], which digests the data stored in an instance of the class into a single hash value (a 32-[[bit]] [[integer (computer science)|signed integer]]). This hash is used by other code when storing or manipulating the instance – the values are intended to be evenly distributed for varied inputs in order to use in [[hash table#Choosing a good hash function|clustering]]. This property is important to the performance of [[hash table]]s and other [[data structure]]s that store objects in groups ("buckets") based on their computed hash values. Technically, in Java, <tt>hashCode()</tt> by default is a native method, meaning, it has the modifier 'native', as it is implemented directly in the native code in the JVM.
| |
| | |
| ==<tt>hashCode()</tt> in general==
| |
| All the classes inherit a basic hash scheme from the fundamental base class <tt>java.lang.Object</tt>, but instead many override this to provide a hash function that better handles their specific data. Classes which provide their own implementation must override the object method <tt>public int hashCode()</tt>.
| |
| | |
| The general [[design by contract|contract]] for overridden implementations of this method is that they behave in a way consistent with the same object's <tt>equals()</tt> method: that a given object must consistently report the same hash value (unless it is changed so that the new version is no longer considered "equal" to the old), and that two objects which <tt>equals()</tt> says are equal ''must'' report the same hash value. There's no requirement that hash values be consistent between different Java implementations, or even between different execution runs of the same program, and while two ''unequal'' objects having different hashes is very desirable, this is not mandatory (that is, the hash function implemented need not be a [[perfect hash function|perfect hash]]).<ref name="oracle_objectdoc">[http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#hashCode%28%29 java.lang.Object.hashCode() documentation], Java SE 1.5.0 documentation, Oracle Inc.</ref>
| |
| | |
| For example, the class <tt>Employee</tt> might implement its hash function by composing the hashes of its members:
| |
| <source lang="java">
| |
| public class Employee {
| |
| int employeeId;
| |
| String name;
| |
| Department dept;
| |
| | |
| // other methods would be in here
| |
| | |
| @Override
| |
| public int hashCode() {
| |
| int hash = 1;
| |
| hash = hash * 17 + employeeId;
| |
| hash = hash * 31 + name.hashCode();
| |
| hash = hash * 13 + (dept == null ? 0 : dept.hashCode());
| |
| return hash;
| |
| }
| |
| }
| |
| </source>
| |
| | |
| ==The <tt>java.lang.String</tt> hash function==
| |
| In an attempt to provide a fast implementation, early versions of the Java <tt>String</tt> class provided a <tt>hashCode()</tt> implementation that considered at most 16 characters picked from the string. For some common data this worked very poorly, delivering unacceptably clustered results and consequently slow hashtable performance.<ref name="Bloch">Bloch</ref>
| |
| | |
| From Java 1.2, <tt>java.lang.String</tt> class implements its <tt>hashCode()</tt> using a product sum algorithm over the entire text of the string.<ref name="Bloch" /> An instance <code>s</code> of the <code>java.lang.String</code> class, for example, would have a hash code <math>h(s)</math> defined by
| |
| | |
| :<math>h(s)=\sum_{i=0}^{n-1}s[i] \cdot 31^{n-1-i}</math> | |
| | |
| where terms are summed using Java 32-bit <code>int</code> addition, <math>s[i]</math> denotes the <math>i</math>th character of the string, and <math>n</math> is the length of <code>s</code>.<ref name="javadoc">[http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#hashCode%28%29 java.lang.String.hashCode() documentation], Java SE 1.5.0 documentation, Oracle Inc.</ref>
| |
| <ref name="williams">[http://www.cogs.susx.ac.uk/courses/dats/notes/html/node114.html Choice of hash function -> The String hash function"], Data Structures course notes (2006), Peter M Williams, [[University of Sussex]] School of Information </ref>{{Dead link|date=August 2013}}
| |
| | |
| ==References== | |
| *"Always override hashCode when you override equals" in {{Citation
| |
| | last =Bloch
| |
| | first =Joshua
| |
| | author-link =Joshua Bloch
| |
| | year =2008
| |
| | title =Effective Java
| |
| | edition =2
| |
| | publisher =Addison-Wesley
| |
| | isbn =978-0-321-35668-0
| |
| }}
| |
| | |
| <references />
| |
| | |
| ==External links==
| |
| * [http://www.ibm.com/developerworks/java/library/j-jtp05273.html "Java theory and practice: Hashing it out"], Brian Goetz, IBM Developer Works article, 27 May 2003
| |
| * [http://www.javamex.com/tutorials/collections/hash_function_technical.shtml "How the String hash function works (and implications for other hash functions)"], Neil Coffey, Javamex
| |
| | |
| [[Category:Java programming language]]
| |
| [[Category:Hashing]]
| |
| [[Category:Hash functions]]
| |
| [[Category:Checksum algorithms]]
| |
Hi there, I am Alyson Pomerleau and I think it seems quite good when you say it. He is an information officer. Ohio is where his home is and his family members enjoys it. I am truly fond of to go to karaoke but I've been taking on new issues lately.
Have a look at my homepage :: psychic phone - see post -