2016-04-11 14 views
5
/** 
    * Computes key.hashCode() and spreads (XORs) higher bits of hash 
    * to lower. Because the table uses power-of-two masking, sets of 
    * hashes that vary only in bits above the current mask will 
    * always collide. (Among known examples are sets of Float keys 
    * holding consecutive whole numbers in small tables.) So we 
    * apply a transform that spreads the impact of higher bits 
    * downward. There is a tradeoff between speed, utility, and 
    * quality of bit-spreading. Because many common sets of hashes 
    * are already reasonably distributed (so don't benefit from 
    * spreading), and because we use trees to handle large sets of 
    * collisions in bins, we just XOR some shifted bits in the 
    * cheapest possible way to reduce systematic lossage, as well as 
    * to incorporate impact of the highest bits that would otherwise 
    * never be used in index calculations because of table bounds. 
    */ 

static final int hash(Object key) { 
    int h; 
    return (key == null) ? 0 : (h = key.hashCode())^(h >>> 16); 
} 

poniżej jest wcześniejsza wersja JDK 1.6zrozumienie metoda komentarz dla hash() klasy HashMap w java 8

/** 
    * Applies a supplemental hash function to a given hashCode, which 
    * defends against poor quality hash functions. This is critical 
    * because HashMap uses power-of-two length hash tables, that 
    * otherwise encounter collisions for hashCodes that do not differ 
    * in lower bits. Note: Null keys always map to hash 0, thus index 0. 
    */ 
    static int hash(int h) { 
     // This function ensures that hashCodes that differ only by 
     // constant multiples at each bit position have a bounded 
     // number of collisions (approximately 8 at default load factor). 
     h ^= (h >>> 20)^(h >>> 12); 
     return h^(h >>> 7)^(h >>> 4); 
    } 

może ktoś wyjaśnić, jakie są korzyści z tego stosujące ten rodzaj mieszania niż to zostało zrobione we wcześniejszych wersjach java. Jak wpłynie to na prędkość i jakość dystrybucji kluczy i mam na myśli nową funkcję skrótu zaimplementowaną w jdk 8 i jak do tego doszło, aby zmniejszyć kolizje?

+1

Czy możesz dołączyć fragment kodu opisujący sposób jego wykonania we wcześniejszych wersjach? W szczególności mogą występować różne implementacje w różnych wersjach. Które dokładnie masz na myśli? –

+0

http://stackoverflow.com/questions/30225054/why-is-there-a-transformation-of-hashcode-to-get-hash-and-is-it-a-good-idea i http://stackoverflow.com/questions/33177043/why-and-how-does-hashmap-have-own-internal-implementation-of-hashcode-call/33177236 – Tom

+0

@tobias_k edytował pytanie, aby uwzględnić poprzednią wersję hashing. –

Odpowiedz

2

W sytuacjach, gdy metoda hashCode jest dość źle zachowana, wydajność HashMap może ulec drastycznej degradacji. Na przykład, powiedzmy, że twoja metoda hashCode wygenerowała tylko numer bitu .

Rozwiązuje to problem przez xor, wprowadzając kod skrótu z przesuniętym w prawo 16. Jeśli numer był dobrze rozłożony, to powinno być. Jeśli był zły, powinien go poprawić.

+0

Ale jak doszliśmy do zmiany 16-bitowej w prawo, a dlaczego nie, 32 czyni to skutecznym. Chcesz wiedzieć, w jaki sposób to zrobiliśmy –