HashTable中的内部数组的初始容量是11,加载因子也是0.75数组的增容方式为(oldCapacity * 2 + 1):
Java代码
public Hashtable() {
this(11, 0.75f);
}
protected void rehash() {
int oldCapacity = table.length;
Entry[] oldMap = table;
int newCapacity = oldCapacity * 2 + 1;
} |
第六个不同我们从它们两个遍历方式的内部实现上来说。
Hashtable HashMap都使用了 Iterator。而由于历史原因,Hashtable还使用了Enumeration的方式 。
小花:Iterator和Enumeration的区别是什么啊?给我讲讲。
小胖:我不是说我没有时间嘛,下回的。
小花:我都记下来,省得你给我混过去。(拿起笔开始记账中)
小胖:……(紧张)
第七个不同时它们的拷贝构造函数的不同。
依然是通过查看源码,可以发现它们两个对于拷贝函数初始容量的不同值。
HashMap的实现是:
Java代码
public HashMap(Map extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
putAllForCreate(m);
} |
而Hashtable的实现是:
Java代码
public Hashtable(Map extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f);
putAll(t);
} |