简体   繁体   English

键条目在HashMap中为空字符串或null

[英]key Entry as Empty String or null in HashMap

In Hashmap for null key the index is 0 but for Empty string what will be the index. 在Hashmap中,对于null键,索引为0,但对于Empty string,索引将为0。 I debug it and found that it is creating a linkedlist at the 0th index and storing both value there. 我对其进行调试,发现它正在第0个索引处创建一个链表并将两个值存储在那里。

So why empty string value is storing in the 0th position and if it is calculating the index using the hashmap of the empty string then what will the hashcode of empty string. 那么,为什么空字符串值存储在第0个位置,并且如果它使用空字符串的哈希图计算索引,那么空字符串的哈希码将是什么。

HashMap<String, String> hm= new HashMap<>();
hm.put("", "");
hm.put(null, null);

The hash code of the empty string will be 0 at least in the Oracle's Java 8 implementation. 空字符串的哈希码至少在Oracle的Java 8实现中为0

This is an extract of the source of the `java.util.HashMap class in Java 1.8 of the method used to calculate the hash: 这是Java 1.8中用于计算哈希的方法的`java.util.HashMap类的源代码的摘录:

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

This is the relevant method which calculates the hash. 这是计算哈希的相关方法。

So in essence this is how it calculates the hash code of the empty string: 因此,从本质上讲,这是它计算空字符串的哈希码的方式:

System.out.println(("".hashCode()) ^ ("".hashCode() >>> 16));

The hash code of null will be 0 anyway. 无论如何, null的哈希码将为0 See the code above with the hash method. 请参见上面带有hash方法的代码。

Because hashcode for an empty string returns 0, and that it is the same value for a null object. 因为空字符串的哈希码返回0,并且它与空对象的值相同。 So you have a collision of hash, so it goes to the same cell. 因此,您遇到了哈希冲突,因此它进入了同一单元格。

* At least for the current implementation in the class String - that could change one day *至少对于String类中的当前实现-可能会改变一天

Hash codes in Java have to meet the following requirement : Java中的哈希码必须满足以下要求

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. 在Java应用程序的执行过程中,只要在同一对象上多次调用它,则hashCode方法必须一致地返回相同的整数,前提是未修改该对象的equals比较中使用的信息。 This integer need not remain consistent from one execution of an application to another execution of the same application. 从一个应用程序的执行到同一应用程序的另一执行,此整数不必保持一致。

Note the last sentence. 注意最后一句话。 This allows for hash codes to be unpredictable between different runs of the same application, which protects against a certain class of denial of service attacks. 这使得在同一应用程序的不同运行之间哈希码是不可预测的,从而防止了某种拒绝服务攻击。

So there is no way to predict what the hash code of any particular object (be it null or an actual object) will be. 因此,无法预测任何特定对象(它为null还是实际对象)的哈希码。 You only know that it'll be the same for the same object and for objects that are equal to it according to the equals method. 您只知道对于相同的对象以及根据equals方法等于该对象的对象来说,它将是相同的。

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

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