JDK 1.8 Hashtable的源码分析
/**
* Hashtable特点:
* 与hashTable一样 1.1 效率低,线程安全,key 不为null hashMap1.2 效率高,key为null 长度11
*/
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable {
/**
*hash表中的数据 table
*/
private transient Entry<?,?>[] table;
/**
* 集合大小
*/
private transient int count;
/**
* 重设大小的阈值 12
*/
private int threshold;
/**
* 加载因子
*
* @serial
*/
private float loadFactor;
// 初始值 长度11 加载因子0.75
public Hashtable() {
this(11, 0.75f);
}
// 线程安全
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
}
* Hashtable特点:
* 与hashTable一样 1.1 效率低,线程安全,key 不为null hashMap1.2 效率高,key为null 长度11
*/
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable {
/**
*hash表中的数据 table
*/
private transient Entry<?,?>[] table;
/**
* 集合大小
*/
private transient int count;
/**
* 重设大小的阈值 12
*/
private int threshold;
/**
* 加载因子
*
* @serial
*/
private float loadFactor;
// 初始值 长度11 加载因子0.75
public Hashtable() {
this(11, 0.75f);
}
// 线程安全
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index];
for(; entry != null ; entry = entry.next) {
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
addEntry(hash, key, value, index);
return null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器