| HashMap | HashTable |
| HashMap中的hash数组的默认大小是16,而且一定是2的指数。 | HashTable中hash数组默认大小是11,增加的方式是 old*2+1 |
| HashMap非同步的(异步) | HashTable的方法是同步的 |
| HashMap允许null值(key和value都可以,但只允许一个) | HashTable不允许null值(key和value都不可以) |
| HashMap循环使用Iterator | HashTable循环使用Enumeration |
| HashMap不是线程安全的 | HashTable是线程安全的一个Collection |
| HashMap继承自AbstractMap类 | Hashtable继承自Dictionary类 |
相同点:都是无序排列的,都实现了Map接口。

部分源码:
HashMap:
public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V>,
private static final int DEFAULT_SIZE = 16;
public Object clone() {
Hashtable:
public class Hashtable<K, V> extends Dictionary<K, V> implements Map<K, V>,
public Hashtable() {
this(11);
}
public synchronized Object clone() {
本文深入探讨了Java集合框架中的HashMap与HashTable的区别,包括它们的同步性、线程安全、允许null值、迭代器使用以及内部实现细节如hash数组大小策略等。
293

被折叠的 条评论
为什么被折叠?



