ITeye博客

JDK源码学习-HashMap\Hashtable

kayban · 2011-10-08 09:23 · jdk学习 · 编程语言 · Markdown

1、HashMap初始化容量


   // Find a power of 2 >= initialCapacity
   int capacity = 1;
   while (capacity < initialCapacity)
            capacity <<= 1;

   this.loadFactor = loadFactor;
   threshold = (int)(capacity * loadFactor);
   table = new Entry[capacity];

注:初始容量是一个不小于initialCapacity且为2的倍数的数值,默认为16。 2、HashMap扩容resize()


 if (size++ >= threshold)
            resize(2 * table.length);

注:以2倍形式扩容

3、Hashtable初始化

```java

public Hashtable() {
	this(11, 0.75f);
    }

```

默认初始容量为11,加载因子为0.75
提供带参数的构造函数,初始容量区别于HashMap,没有2倍的限制。
4、Hashtable是同步的。
5、Hashtable不能存储null对象

```java

// Make sure the value is not null
	if (value == null) {
	    throw new NullPointerException();
	}

```

6、Hashtable扩容

```java

int newCapacity = oldCapacity * 2 + 1;

```

7、计算数组index的方法

```java

 index = (hash & 0x7FFFFFFF) % tab.length;

```

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。