Hashcode Methods for Java

Source: Internet
Author: User

0 Overview of Hashcode methods for Java
In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data sto Red in an instance of the class to a single hash value (a 32-bit signed integer). Technically, in Java, hashcode () By default was a native method, meaning, it had the modifier ' native ', as it is I Mplemented directly in the native code in the JVM. (using the native keyword means that this method is a native function, that is, the method is implemented in C/s + + language, and is compiled into a DLL, called by Java.) Native keyword Introduction)
All the classes inherit a basic hashing scheme from the fundamental base class Java.lang.Object, but instead many ov Erride 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 ().
1 hashcode () of the object class
hashcode ()
returns the hash code value of the object. This method is supported to improve the performance of a hash table (for example, java.util.Hashtable a hash table provided).

hashCodeThe General agreement is:

    • During Java application execution, when the hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the object to equals is not modified. The integer does not need to be consistent from one execution of an application to another execution of the same application.
    • If two objects are equal according to the equals (object) method, the method that is called for each object in both objects hashCode must produce the same integer result.
    • If equals(java.lang.Object) two objects are not equal according to the method, calling the hashcode method on any of these two objects does not require that a different integer result be generated. However, programmers should be aware that generating different integer results for unequal objects can improve the performance of the hash table.

in fact, the Hashcode method defined by the object class does return different integers for different objects. (This is typically done by converting the object's internal address to an integer, but the JAVATM programming language does not require this implementation technique.) an explanation of the Equals method and the Hashcode method is attached.

2 Common classes of hashcode ()2.1 Integer class of Hashcode ()
public int hashcode ()
     Overrides: Object in a classhashCode
     Returns: the hash code value of the object whose value is the numeric value of the  Integer base int type that the object represents .
2.2 hashcode () of the String class
public int hashcode ()
     Overrides: Object in a classhashCode
     Returns: the hash code value of the object. Stringthe hash code of the object is calculated according to the following formula:
namely: H (s) =s[0]*31^ (n-1) + s[1]*31^ (n-2) + ... + s[n-1];
     Using the int algorithm, this is the I character of the string, which is the length of the s[i] n string, and the ^ represents exponentiation. (The hash value of the empty string is 0)

Hashcode Methods for Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.