Java-HashTable
HashTable<K,V>:
1.数据结构:哈希表(数组+链表+红黑树Jdk1.8之后)
Entry数组:
-
private transient Entry<k,v>[] table;
-
Entry<K,V>链表节点: * private static class Entry<K,V> implements Map.Entry<K,V> { final int hash; final K key; V value; Entry<K,V> next; protected Entry(int hash, K key, V value, Entry<K,V> next) { this.hash = hash; this.key = key; this.value = value; this.next = next; } @SuppressWarnings("unchecked")
2.初始化容量:11
3.默认加载因子:0.75
public Hashtable() {
this(11, 0.75f);
}
4.扩容方式:2倍+1
5.key和value不允许为null
6.线程安全
面试题*synchronized会使它的效率变低,所以我们不使用HashTable
面试考点——API:与新的集合实现不同, Hashtable是同步的。 如果不需要线程安全的实现,建议使用HashMap代替Hashtable 。
如果需要线程安全的并发实现,那么建议使用ConcurrentHashMap代替Hashtable
public class HashTableTest01 {
public static void main(String[] args) {
//java.lang.NullPointerException
Hashtable<Integer,Integer> table = new Hashtable<>();
table.put(null, 1);
table.put(1, null);
System.out.println(table);
}
}