简体   繁体   English

什么是Java 8中String键的替代散列?

[英]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. Java 8为String键提供备用散列,以在遇到大量密钥散列码冲突时提高性能。 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 : 为了更好地解决这个问题,已从JDK 8中删除了替代哈希。检查:

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

http://openjdk.java.net/jeps/180 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: HashMap中的哈希(Object key)函数已修改为以下内容,对String对象没有特殊处理:

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 : 来自core-lib-devs@openjkd 这封电子邮件

  • A new interface Hashable32 is introduced. 引入了一个新的接口Hashable32。
  • Hashable32 provides a method hash32() Hashable32提供方法hash32()
  • String implements Hashable32 and hash32() method String实现Hashable32和hash32()方法
  • HashMap et al recognize String and invoke hash32() rather than hashCode() HashMap等识别String并调用hash32()而不是hashCode()

The revisions of the code: 代码的修订版:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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