Understanding of HashCode

Hash

What is hash

the hash, or hash hash translation, is the input of any length (also called pre-mapping), the hash algorithm, to obtain fixed-length output, and the output is the hash value. This conversion is a compression map, the space hash value is typically much smaller than the input space, and as such, different input may get the same hash value. A hash value can not be uniquely determined by the input value. To the book is a simple message of arbitrary length is compressed to a fixed length message digest function.

Feature

The same hash value necessarily the same object, hash values of different objects may be the same.
The mapping of arbitrary length to a fixed length, because the length of the mapped shorter than him, hash conflict it may occur.

Common Hash Functions

Direct access Conormal

Multiplier to the entire law

Square reindeer method

HashCode

HashCode hash is obtained by, as the representative position in the hash table, or the nature of a Hash function to obtain an integer value is a hash value pair. (Different for different algorithms HashCode the image).
Although hashCode represents the position in the Hash table, but this position is obtained by hash function, rather than a hash lookup table get.
Briefly, is the location in the hash table, the position indicated by the object hashCode obtained by the algorithm.
This is why why not use this HashMap to the basic data types, because no way to get hashCode.

Object.hashCode()

HashCode method call to the image, is a native method, and this was converted to a physical address of the location where the image will be an integer, by calculating the hash function returns an integer value obtained. If two objects equals the same, then the hashcode must be the same, which is why when we define an object, usually rewrite the cause of equals and hashcode.

equals and hashcode:
If you do not override, all the new objects are not the same (different equals, equals default comparison is the address of two references to objects , so be sure, but generally because we believe value can be equal, then it would override the equals method, and because hashcode and equals are related, so they will override the hashcode method).
If two objects equals the same, then the hashcode necessarily the same.
If two objects of different hashcode, then equals must be different.

Objects.hashCode(),Arrays.hashCode()

 public static int hash(Object... values) {
        return Arrays.hashCode(values);
    }

//这个是Array.hashCode()    
public static int hashCode(Object a[]) {
        if (a == null)
            return 0;

        int result = 1;

        for (Object element : a)
            result = 31 * result + (element == null ? 0 : element.hashCode());

        return result;
    }

Objects.hashCode is carrying the hashCode Object, then calls the appropriate HashCode Depending on the type of the object by converting the parameter Object array, is calculated.

String.hashCode()

    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

The string into a character array, is calculated by the Hash Ascii character code, h = 31 * h + val [i]; Why 31 is disposed, explained later.

Interger.hashCode()

  public static int hashCode(int value) {
        return value;
    }

Direct return value

hashCode role

HashCode calculated, the position of the object can be obtained directly corresponding to the hash table, without the need to find the location by traversing the hash table, to improve the efficiency of targeting.
For example: We have a number of 1000, when the store, if you have stored for 900, 901 when the deposit is necessary to compare the previous 900, and by hashCode does not compare so many times. That is the only Cohen hash conflict. But the efficiency is still high.

equals和hashCode

Why should generally override equals rewrite hahsCode
think that if you override the equals, hashCode did not write, then the two objects at the same time as equals, hashCode then theoretically they should do the same phase, but in fact it is different. So to be rewritten.
The default is to compare two equals reference to an object address , so be sure, but generally because we believe value can be equal, then they would override the equals method, and because hashcode and equals are related, so they will override the hashcode method.

		TestMe me = new TestMe("nan");
        TestMe metwo = new TestMe("nan");
        TestMe mePointer = me;
        System.out.println(me.hashCode());
        System.out.println(mePointer.hashCode());
        System.out.println(metwo.hashCode());
        System.out.println(mePointer.equals(me));
        //输出
        //1956725890
        //1956725890
		//356573597
		//true

Why is 31

The reason for using 31, because he is an odd prime number. If the multiplier factor is an even number, and then overflow multiplication, data will be lost, since the shift operation is equivalent to multiplying 2 (low complement 0).

  • The reason for using a prime number is multiplied by a prime number, it is less prone to conflict. Because only one prime factor in and of itself.
    For example: 7 and 8:
    when calculating "4 2", the hashCode 7 = 2 + 4 = 18 is; the other 8 2 + 4 = 20;
    when calculating "12 is. 1", the hashCode = 7 . 1. 19 = + 12 is; another 8 1 + 12 = 20, the conflict.

  • The reason 31 is distributed by the decision , the greater, the greater the possibility of overflow, the smaller, the possibility of conflict.

  • 31 has a good performance, i.e., a shift instead of a multiplication and subtraction , better performance can be obtained: 31 * i == (i << 5) - i, VM modern This optimization can be done automatically. This formula can be deduced very simple.

  • This fact can (32 I <<. 5 = I, then 31 is I = I-I <<. 5), such as 63 may be so

  • 31 In order to use the most important thing is performance. Of course, you can also use 63. But the 63 overflow risk is even greater. So 15 it? Think also available. (The larger, the smaller the potential for conflict, but may overflow and larger space; the smaller, the possibility of conflict, spill less likely).

Published 12 original articles · won praise 0 · Views 189

Guess you like

Origin blog.csdn.net/N_a_n/article/details/105192791