Hashcode of A String in Java

Source: Internet
Author: User

Many of the Java programmers know what's hashcode ' means, but don ' t really know how exactly it's calculated and why Used to calculate the hashcode. Below is the code snippet from Java 1.6, which calculates the hashcode for a string:

 public  int   Hashcode () { int  H = hash;  if  (h = = 0 int  off = offset;  char  val[] = int  len = count;  for  (int  i = 0; i < len; I++) {h  = 31*h + Val[off ++ = H;}  return   H;}  

After reading-a bit, I wrote a sample test Java program, to find the hashcode of a string by multiplying by (which I s the same as shifting left (bitwise) by 5 times and subtracting, as in (I << 5)-i). Below is the sample test program:

 Public classTesthash { Public Static voidMain (string[] args) {String str1= "What the heck?"; intHashcode1 = 0;intHashcode2 = 0;  for(intI=0;i<str1.length (); i++) {Hashcode1= 31*hashcode1 +Str1.charat (i); Hashcode2= (Hashcode2 << 5)-Hashcode2 +Str1.charat (i);} System.out.println ("Hashcode1:" +hashcode1); System.out.println ("Hashcode2:" +hashcode2);}}

The output for this program is:


1. What exactly does this piece of code mean?

Even if someone knows why used, there are a lot of stuff to know about ' Hashing ', ' Hash collisions ' and multiple algo Rithms related to calculating hash values. First off, its a known fact, there is no perfect hashing algorithm, for which there is no collisions. But there is several algorithms, which minimize the collisions and is good enough to use. Now, coming-to-why-is-used in calculating hashcode, the "The reason given by Joshua Bloch," in the book ' Effective Ja VA ':

Effective Java says this: 31 is chosen because it is an odd prime, and if the multiplier is even, and the multiplication overflows, the information is lost because multiplying with 2 is equivalent to the shift operation. The benefits of using prime numbers are not obvious, but it is customary to use prime numbers to calculate hash results. 31 There is a good feature, that is, using shift and subtraction instead of multiplication, you can get better performance:31*i== (i<<5)-I. Today's VMs can automatically do this optimization.

2. What are the characteristics of the hashcode it returns?

As you can see, the string class uses its value values as arguments to calculate hashcode, that is, the same value must have the same hashcode values. This is also easy to understand, because the value values are the same, then the equals comparison is equal, the Equals method is equal, then the hashcode must be equal. The reverse is not necessarily true. It does not guarantee that the same hashcode must have the same object.

A good hash function should be this: generating unequal hashcode for different objects.

Ideally, the hash function should distribute the unequal instances of the set evenly across all possible hashcode, which would be very difficult to achieve, at least not in Java. Because we can see that hashcode is a non-random generation, it has a certain regularity, that is, the above mathematical equation, we can construct some with the same hashcode but the value is not the same, such as: AA and BB hashcode is the same.

Speaking of which, you might think that the original structure of the hash conflict is so simple ah, I can not hashmap function constructs a lot of <key,value> not all the same, but with the same hashcode, so that the HashMap function can be turned into a one-way linked list, How long does the runtime change from linear to square? Although the Hashcode method that HashMap overrides is more complex than the string class, it is theoretically possible to do so. This is also the most popular hash collision DOS event recently.

Hashcode method rewritten in HashMap

        Public Final int hashcode () {           return (key==null   ? 0:key.hashcode ()) ^                   (Value= = null ? 0 : Value.hashcode ());        }




Hashcode of A String in Java

Related Article

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.