找到你要的答案

Q:how are buckets created in hashmap?

Q:创建在HashMap桶呢?

I am a bit confused about internal working of hash map. I have created a Hashmap with default capacity 16 and my key class always return a hash code value 1. So when i will enter the 13th element to this map it will double the map size.
1. How many buckets will be there in hash map ?
2. Does hash map create a new bucket on demand (i.e When hash code does not match to any existing bucket's hash code value)?

I am a bit confused about internal working of hash map. I have created a Hashmap with default capacity 16 and my key class always return a hash code value 1. So when i will enter the 13th element to this map it will double the map size.
1. How many buckets will be there in hash map ?
2. Does hash map create a new bucket on demand (i.e When hash code does not match to any existing bucket's hash code value)?

answer1: 回答1:

Although it's a bit late but this might help fellow users looking for an answer to this question.

As of your first question, How many buckets will be there in hash map ?-- There will be 16 buckets as that's the default capacity if you don't specify it in the constructor argument while instantiating HashMap.

Now let's come to the second question i.e. Does hash map create a new bucket on demand (i.e When hash code does not match to any existing bucket's hash code value)?

It will be interesting to look at the implementation of Hashmap's put method here.

public V put(K key, V value) {

    if (key == null)
        return putForNullKey(value);

    int hash = hash(key.hashCode());
    int i = indexFor(hash, table.length);

    for (Entry<K, V> e = table[i]; e != null; e = e.next) {
        Object k;

        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }
    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

If you look closely, the method hash which accepts key's hashmap as argument is called inside the put method. The job of this method is to defend against poor quality hash functions . After this indexFor method is used, to identify the index (bucket) where the element will be inserted. The index returned by this method always points to an existing bucket. So, there isn't a case where the key's hashcode points to a bucket that doesn't exist.

So when does a hashmap creates a new bucket? It's neatly explained in javase docs.

It says: "An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets. "

Hope that helps.

虽然有点晚,但这可能会帮助其他用户寻找这个问题的答案。

As of your first question, How many buckets will be there in hash map ?-- There will be 16 buckets as that's the default capacity if you don't specify it in the constructor argument while instantiating HashMap.

现在让我们来讨论第二个问题,即哈希图创建一个新的桶按需(即哈希码不匹配任何现有的桶的哈希代码值)?

再看Hashmap的实施方法这里有意思。

public V put(K key, V value) {

    if (key == null)
        return putForNullKey(value);

    int hash = hash(key.hashCode());
    int i = indexFor(hash, table.length);

    for (Entry<K, V> e = table[i]; e != null; e = e.next) {
        Object k;

        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }
    modCount++;
    addEntry(hash, key, value, i);
    return null;
}

如果你仔细看,它接受关键的HashMap作为参数被称为内把方法散列。此方法的工作是抵御质量低劣的哈希函数。在使用指标的方法,确定指标(桶)里的元素将被插入。此方法返回的索引总是指向现有的桶。所以,没有一种情况下,关键的hashCode点一桶,不存在。

所以当一个HashMap创建一个新的斗?它在JavaSE文档简单的解释。

它说:“HashMap实例有影响其性能的两个参数:初始容量和加载因子。容量是哈希表中的桶数,初始容量仅是哈希表创建时的容量。负载因子是衡量哈希表在其容量自动增加之前得到的充分程度的度量。当哈希表中的条目数超过负荷的因素和当前容量的乘积,哈希表是重复(即内部数据结构重建),哈希表有大约两倍的存储桶的数目。”

希望帮助。

answer2: 回答2:

When you create a HashMap with the default capacity (16), you create it with 16 buckets (i.e., the capacity == the number of buckets).

  1. When the capacity is doubled, the number of buckets is doubled.

  2. The hashCode always matches some existing bucket, since modulus N (where N is the current capacity) is applied on the computed hash in order to find the bucket it belongs to.

当你创建一个默认的容量HashMap(16),你创建了16桶(即容量= =桶的数量)。

  1. 当容量加倍时,桶数增加一倍。

  2. hashCode总是与一些现有的桶,因为模数N(其中N是电流容量)应用于计算的散列为了寻找属于它的桶。

answer3: 回答3:

I'm more from C#, but Java is quite similar to it :) So forgive me if anythings wrong here. Feel free to correct it.

The number of available buckets can't be told easily. It depends on the number of elements in the map (load factor). The load factor is kept about 75% of the total capacity. This is related to the fact, that if there are two identical hashes one of them has to be rehashed (to get another position in the map) -> 75% is a good tradeoff between performance and size. Whenever there are more than 75% the size is doubled (i.e. 75% of 16 is 12; with the 13th element the size is doubled). And anyhow this is also an answer to the second question.

I'm more from C#, but Java is quite similar to it :) So forgive me if anythings wrong here. Feel free to correct it.

可用桶的数量不能轻易地告诉。它取决于图中元素的数目(负载因子)。负载因数保持在总容量的75%左右。这是事实,如果有两个相同的哈希值,其中一个已经被改变(进入地图的另一个位置)- & gt;75%是性能和尺寸之间的一个很好的权衡。每当有超过75%的大小是一倍(即75%的16是;与第十三个元素的大小是一倍)。总之,这也是第二个问题的答案。

java  collections