HashMap和HashTable的区别以及使用
HashMap
- 底层由数组和链表实现,可以存储null键和null值,实现不同步所以线程不安全,映射不时有序的
- 初始容量为16,扩容方法:newsize = oldsize*2,所以容量一定为2的n次幂
- 扩容针对整个Map,每次扩容时,原来数组中的元素依次重新计算存放位置,并重新插入
- 插入元素后才判断该不该扩容,有可能无效扩容(插入后如果扩容,所以如果没有再次插入,就会产生无效扩容)
- 当Map中元素总数超过Entry数组的75%,触发扩容操作,为了减少链表长度,元素分配更均匀
- 计算index方法:index = hash & (tab.length – 1)
HashMap的构造函数
// 默认构造函数。
HashMap()
// 指定“容量大小”的构造函数
HashMap(int capacity)
// 指定“容量大小”和“加载因子”的构造函数
HashMap(int capacity, float loadFactor)
// 包含“子Map”的构造函数
HashMap(Map<? extends K, ? extends V> map)
HashMap的API
//清空整个表
void clear()
//返回此HashMap实例的浅拷贝:未克隆键和值本身
Object clone()
//如果表中含有特定key值则返回true
boolean containsKey(Object key)
//如果表中含有特定value值则返回true
boolean containsValue(Object value)
//返回键值对的集合
Set<Map.Entry<K,V>> entrySet()
//返回指定键映射到的值,如果此映射不包含键的映射,则返回null。
V get(Object key)
//如果此映射不包含键 - 值映射,则返回true
boolean isEmpty()
//返回HashMap中的键值集合
Set<K> keySet()
//存入新的键值对
V put(K key, V value)
//复制特定的Map表
void putAll(Map<? extends K,? extends V> m)
//移除特定key值的映射
V remove(Object key)
//返回Map表中映射的数量
int size()
//返回Map表中的Collection集合
Collection<V> values()
Hashtable
- 底层数组+链表实现,无论key还是value都不能为null,线程安全,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相关优化
- 初始size为11,扩容:newsize = olesize*2+1
- 计算index的方法:index = (hash & 0x7FFFFFFF) % tab.length
Hashtable的构造函数
// 默认构造函数。
public Hashtable()
// 指定“容量大小”的构造函数
public Hashtable(int initialCapacity)
// 指定“容量大小”和“加载因子”的构造函数
public Hashtable(int initialCapacity, float loadFactor)
// 包含“子Map”的构造函数
public Hashtable(Map<? extends K, ? extends V> t)
Hashtable的API
void clear()
//Clears this hashtable so that it contains no keys.
Object clone()
//Creates a shallow copy of this hashtable.
boolean contains(Object value)
//Tests if some key maps into the specified value in this hashtable.
boolean containsKey(Object key)
//Tests if the specified object is a key in this hashtable.
boolean containsValue(Object value)
//Returns true if this hashtable maps one or more keys to this value.
Enumeration<V> elements()
//Returns an enumeration of the values in this hashtable.
Set<Map.Entry<K,V>> entrySet()
//Returns a Set view of the mappings contained in this map.
boolean equals(Object o)
//Compares the specified Object with this Map for equality, as per the definition in the Map interface.
V get(Object key)
//Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
int hashCode()
//Returns the hash code value for this Map as per the definition in the Map interface.
boolean isEmpty()
//Tests if this hashtable maps no keys to values.
Enumeration<K> keys()
//Returns an enumeration of the keys in this hashtable.
Set<K> keySet()
//Returns a Set view of the keys contained in this map.
V put(K key, V value)
//Maps the specified key to the specified value in this hashtable.
void putAll(Map<? extends K,? extends V> t)
//Copies all of the mappings from the specified map to this hashtable.
protected void rehash()
//Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently.
V remove(Object key)
//Removes the key (and its corresponding value) from this hashtable.
int size()
//Returns the number of keys in this hashtable.
String toString()
//Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
Collection<V> values()
//Returns a Collection view of the values contained in this map.
参考文献:
[1]https://www.jianshu.com/p/034315b201b6
[2]https://www.cnblogs.com/skywang12345/p/3310887.html
[3]https://blog.csdn.net/qq_32635069/article/details/79798741
[4]http://tool.oschina.net/apidocs/apidoc?api=jdk_7u4
[5]https://www.cnblogs.com/heyonggang/p/9112731.html