public int cacheIdx ( String key ) { int h = 0 ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { h = 31 * h + key . charAt ( i ) ; } if ( h < 0 ) h *= - 1 ; return h % segSize ; }
Taken from String Hash but coded to ensure consistent across Java versions . Also covers negative case ;