Sponsored Links
-->

Saturday, December 30, 2017

equals() and hashCode() in Java with an Example - YouTube
src: i.ytimg.com

In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit 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 for use in clustering. This property is important to the performance of hash tables and other data structures that store objects in groups ("buckets") based on their computed hash values. Technically, in Java, hashCode() by default is a native method, meaning, it has the modifier 'native', as it is implemented directly in the native code in the JVM.


Video Java hashCode()



hashCode() in general

All the classes inherit a basic hash scheme from the fundamental base class java.lang.Object, 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 public int hashCode().

The general contract for overridden implementations of this method is that they behave in a way consistent with the same object's equals() 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 equals() 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 doesn't need to be a perfect hash).

For example, the class Employee might implement its hash function by composing the hashes of its members:


Maps Java hashCode()



The java.lang.String hash function

In an attempt to provide a fast implementation, early versions of the Java String class provided a hashCode() 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.

From Java 1.2, java.lang.String class implements its hashCode() using a product sum algorithm over the entire text of the string. An instance s of the java.lang.String class, for example, would have a hash code h ( s ) {\displaystyle h(s)} defined by

h ( s ) = ? i = 0 n - 1 s [ i ] ? 31 n - 1 - i {\displaystyle h(s)=\sum _{i=0}^{n-1}s\left[\,i\,\right]\cdot 31^{n-1-i}}

where terms are summed using Java 32-bit int addition, s [ i ] {\displaystyle s\left[\,i\,\right]} denotes the UTF-16 code unit of the i {\displaystyle i} th character of the string, and n {\displaystyle n} is the length of s.


Overriding equals() and hashCode() method in Java and Hibernate
src: 2.bp.blogspot.com


See also

  • Non-cryptographic hash functions

Java 027 - Programação Orientada a Objetos (Hashcode ) - Definindo ...
src: i.ytimg.com


References

  • "Always override hashCode when you override equals" in Bloch, Joshua (2008), Effective Java (2nd ed.), Addison-Wesley, ISBN 978-0-321-35668-0 

How does Java HashMap or LinkedHahsMap handles collisions?
src: 2.bp.blogspot.com


External links

  • Goetz, Brian (2017-06-02). "Java theory and practice: Hashing it out"". IBM Developer Works (published 2003-05-27). 
  • Coffey, Neil (2017-06-02). "How the String hash function works (and implications for other hash functions)". Javamex. 
  • "Why should hash functions use a prime number modulus?". stackoverflow. 2017-06-02. 
  • "What is a sensible prime for hashcode calculation?". stackoverflow. 2017-06-02. 
  • Khojaye, Muhammad (2017-06-02). "Java Hashing" (published 2010-02-05). 
  • Navarro, Galo (2017-06-02). "How does the default hashCode() work?". github.io (published 2017-01-30). 

Source of article : Wikipedia