简体   繁体   中英

What is alternative hashing for String keys in Java 8?

Java 8 is providing alternative hashing for String keys to improve performance when a large number of key hash code collisions are encountered. Can anybody explain what is that and how it will work?

To bring more relevance to this question, the alternative hashing has been removed from JDK 8. Check out :

http://docs.oracle.com/javase/8/docs/technotes/guides/collections/changes8.html

http://openjdk.java.net/jeps/180

It is interesting to note that once the number of items in a hash bucket grows beyond a certain threshold, that bucket will switch from using a linked list of entries to a balanced tree.

The hash(Object key) function in the HashMap has been revised to follows with no special treatment to String objects:

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

From this email of core-lib-devs@openjkd :

  • A new interface Hashable32 is introduced.
  • Hashable32 provides a method hash32()
  • String implements Hashable32 and hash32() method
  • HashMap et al recognize String and invoke hash32() rather than hashCode()

The revisions of the code:

值得注意的是,转向MurmurHash3不会阻止DoS攻击: http//emboss.github.com/blog/2012/12/14/breaking-murmur-hash-flooding-dos-reloaded/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM