第3章-002节

一、构造⽅法

Hashtable⼀共提供了4个构造⽅法:
public Hashtable(int initialCapacity, float loadFactor): ⽤指定初始容量和指定加载因⼦构
造⼀个新的空哈希表。useAltHashing为boolean,其如果为真,则执⾏另⼀散列的字符串
键,以减少由于弱哈希计算导致的哈希冲突的发⽣。
public Hashtable(int initialCapacity):⽤指定初始容量和默认的加载因⼦ (0.75) 构造⼀个新
的空哈希表。
public Hashtable():默认构造函数,容量为11,加载因⼦为0.75。
public Hashtable(Map<? extends K, ? extends V> t):构造⼀个与给定的 Map 具有相同映
射关系的新哈希表。
/**
* Constructs a new, empty hashtable with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hashtable.
* @param loadFactor the load factor of the hashtable.
* @exception IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive.
/
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
if (initialCapacity==0)
initialCapacity = 1;
this.loadFactor = loadFactor;
table = new Entry[initialCapacity];
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1) ;
useAltHashing = sun.misc.VM.isBooted() &&
(initialCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
}
/*

* Constructs a new, empty hashtable with the specified initial capacity
* and default load factor (0.75).
*
* @param initialCapacity the initial capacity of the hashtable.
* @exception IllegalArgumentException if the initial capacity is less
* than zero.
/
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
/*

* Constructs a new, empty hashtable with a default initial capacity (11)
* and load factor (0.75).
/
public Hashtable() {
this(11, 0.75f);
}
/*

* Constructs a new hashtable with the same mappings as the given
* Map. The hashtable is created with an initial capacity sufficient to
* hold the mappings in the given Map and a default load factor (0.75).
*
* @param t the map whose mappings are to be placed in this map.
* @throws NullPointerException if the specified map is null.
* @since 1.2
/
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2
t.size(), 11), 0.75f);
putAll(t);
}

二、put⽅法

put⽅法的整个流程为:
判断value是否为空,为空则抛出异常;
计算key的hash值,并根据hash值获得key在table数组中的位置index,如果table[index]元素
不为空,则进⾏迭代,如果遇到相同的key,则直接替换,并返回旧value;
否则,我们可以将其插⼊到table[index]位置。
public synchronized V put(K key, V value) {
// Make sure the value is not null确保value不为null
if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
//确保key不在hashtable中
//⾸先,通过hash⽅法计算key的哈希值,并计算得出index值,确定其在table[]中的位置
//其次,迭代index索引位置的链表,如果该位置处的链表存在相同的key,则替换value,返
回旧的value
Entry tab[] = table;
int hash = hash(key);
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
V old = e.value;
e.value = value;
return old;
}
}
modCount++;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
//如果超过阀值,就进⾏rehash操作
rehash();
tab = table;
hash = hash(key);
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
//将值插⼊,返回的为null
Entry<K,V> e = tab[index];
// 创建新的Entry节点,并将新的Entry插⼊Hashtable的index位置,并设置e为新的Entry
的下⼀个元素
tab[index] = new Entry<>(hash, key, value, e);
count++;
return null;
}
通过⼀个实际的例⼦来演示⼀下这个过程:
假设我们现在Hashtable的容量为5,已经存在了(5,5),(13,13),(16,16),(17,17),(21,21)这5个
键值对,⽬前他们在Hashtable中的位置如下:
图片说明
现在,我们插⼊⼀个新的键值对,put(16,22),假设key=16的索引为1.但现在索引1的位置有两个
Entry了,所以程序会对链表进⾏迭代。迭代的过程中,发现其中有⼀个Entry的key和我们要插⼊
的键值对的key相同,所以现在会做的⼯作就是将newValue=22替换oldValue=16,然后返回
oldValue=16.
图片说明
然后我们现在再插⼊⼀个,put(33,33),key=33的索引为3,并且在链表中也不存在key=33的
Entry,所以将该节点插⼊链表的第⼀个位置。
图片说明
图片说明

大家一起学习丫~

java集合学习 文章被收录于专栏

主要讲解java集合相关的概念、原理和部分面试题。 共计11章。 不断更新中。。。 (摘自某大佬)

全部评论

相关推荐

01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11166次浏览 95人参与
# 你的实习产出是真实的还是包装的? #
1975次浏览 42人参与
# MiniMax求职进展汇总 #
24134次浏览 309人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7656次浏览 43人参与
# 简历第一个项目做什么 #
31761次浏览 341人参与
# 重来一次,我还会选择这个专业吗 #
433583次浏览 3926人参与
# 米连集团26产品管培生项目 #
6050次浏览 216人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187235次浏览 1122人参与
# 牛客AI文生图 #
21453次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152480次浏览 888人参与
# 研究所笔面经互助 #
118978次浏览 577人参与
# 简历中的项目经历要怎么写? #
310397次浏览 4220人参与
# AI时代,哪些岗位最容易被淘汰 #
63899次浏览 828人参与
# 面试紧张时你会有什么表现? #
30521次浏览 188人参与
# 你今年的平均薪资是多少? #
213162次浏览 1039人参与
# 你怎么看待AI面试 #
180188次浏览 1258人参与
# 高学历就一定能找到好工作吗? #
64340次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76557次浏览 374人参与
# 我的求职精神状态 #
448159次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363553次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160687次浏览 1112人参与
# 校招笔试 #
471293次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务