简体   繁体   English

具有两个值相同但存储桶索引不同的键对象的哈希图

[英]Hashmap with two key object having same value but different bucket index

If Hashmap has two keys having same value, for eg: 如果Hashmap具有两个具有相同值的键,例如:

HashMap map=new HashMap();
map.put("a","abc");
map.put("a","xyz");   

So here put two key with "a" value and suppose for first bucketindex=1 and second bucketindex=9 因此,在此放置两个带有“ a”值的键,并假设第一个bucketindex = 1和第二个bucketindex = 9

So my question is if bucket index for both is coming different after applying hashing algorithm, in this how to handle for not inserting duplicate key as it is already present and hashmap cannot have duplicate key. 所以我的问题是,在应用哈希算法后,两者的存储桶索引是否会不同,在这种情况下,如何处理不插入重复键,因为它已经存在并且hashmap不能具有重复键。 please suggest your view on this. 请提出您对此的看法。

There won't be any such thing as "second bucket index". 不会有“第二个存储区索引”之类的东西。

I suggest you add something like System.out.println(map.toString()) in order to see what that second put() has done to your map. 我建议您添加类似System.out.println(map.toString())东西,以查看第二个put()对您的地图做了什么。

EDIT: 编辑:

In the method put(key,value) , the "bucket index" is computed as a function of the key element's value, not the value element's value (so "a" and "a" give the same index for the bucket). put(key,value) ,“存储桶索引”是根据key元素的值而不是value元素的值计算的(因此,“ a”和“ a”为存储桶提供相同的索引)。 This function is supposed to be deterministic so feeding it the same value ("a" in your case), the same hashCode() will come out and subsequently, the same bucket index. 该函数应该是确定性的,因此向其提供相同的值(在您的情况下为“ a”),将出现相同的hashCode(),随后出现相同的存储桶索引。

In Java if a hashing function returns the same hash, equality of two objects is determined by equals() method. 在Java中,如果哈希函数返回相同的哈希,则两个对象的相等性由equals()方法确定。 And if the objects are found equal, the old one is simply replaced by the new one. 如果发现对象相等,则旧对象将简单地替换为新对象。

Instead, if the objects are not equal, they just get chained in a linked list (or a balanced tree) and the map contains both objects, because they are different . 相反,如果对象不相等,则它们只是被链接在一个链表(或平衡树)中,并且地图包含这两个对象, 因为它们是不同的

So, back to your question: "if bucket index for both is coming different after applying hashing algorithm" - this is impossible for equal objects. 因此,回到您的问题:“如果在应用哈希算法后,两者的存储桶索引都不同”,则对于相等的对象来说这是不可能的。 Equal objects must have the same hash code. 相等的对象必须具有相同的哈希码。

To make @Erwin's answer more clear, here's the source code of HashMap from JDK 为了使@Erwin的答案更加清楚,这是来自JDK的HashMap的源代码

public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

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

Digging more deep you will find that the bucket index is calculated from key 's hash code. 深入研究,您会发现bucket索引是根据key的哈希码计算得出的。

To make it simple and straightforward, putting duplicate key with different values to the same HashMap will result just one single entry, which the second put is just overwriting the value of the entry. 为了简单明了,将具有不同值的重复键放入同一HashMap只会得到一个条目,而第二个则只是覆盖条目的值。

如果您的问题是如何创建可为同一个键处理多个值的哈希映射,则您需要的是Map>,以便每次键相同时就向数组列表添加一个新值。

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

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