このプログラムを実行すると
public class MyHashMapOperationsDebug {
public static void main(String[] args) {
MyHashMap hashMap = new MyHashMap();//MyHashMap is replica of HashMap
for (int i=1;i<=11;i++)
hashMap.put(i, i+100);
}
}
そしてMyHashMap.java
持っています
void addEntry(int hash, K key, V value, int bucketIndex) { //replica of HashMap's addEntry method
Entry<K,V> e = table[bucketIndex];
**System.out.println("bucketIndex : " + bucketIndex);**
table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
if (size++ >= threshold)
resize(2 * table.length);
}
出力:
bucketIndex : 7
bucketIndex : 14
bucketIndex : 4
bucketIndex : 13
bucketIndex : 1
bucketIndex : 8
bucketIndex : 2
bucketIndex : 11
bucketIndex : 11
bucketIndex : 2
bucketIndex : 8
サイズ 16 のマップに 11 個のキーしか格納されていない場合でも、一部のキーが同じバケットに移動するのはなぜですか? たとえば、インデックス 2 のバケットと 11 にはそれぞれ 2 つのキーがあります
編集: 以下の入力を読んだ後 1 つの質問: Java の HashMap と Integer が使用されている場合、上記の場合の複雑さはどうなりますか。O(1) ですか?