JDK源码阅读--Hashtable
2024-10-07 22:29:35
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable { Hashtable的默认初始容量是11,加载因子是0.75f。通过synchronized方法保证线程安全。
实现结构与HashMap基本一致。Hashtable的键和值都不能为空。 Hashtable的构造函数:
/**
* 使用指定的初始容量和装载因子构造一个空的Hashtable.
*
* @param initialCapacity hashtable的初始容量
* @param loadFactor hashtable的装载因子
* @exception IllegalArgumentException 如果初始容量小于0,或装载因子是负数,则抛出IllegalArgumentException异常
*/
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);
} /**
* 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);
}
最新文章
- Linux学习之文件操作
- 概率论与数理统计图解.tex
- Vue2随笔
- App.config/Web.config 中特殊字符的处理
- aehyok.com的成长之路一——开篇
- Ecplise + Xdebug 一波三折终于能单步调试了
- SAP B1 ADDON 开发
- Rapha&#235;l 是一个小型的 JavaScript 库,用来简化在页面上显示向量图的工作。你可以用它在页面上绘制各种图表、并进行图片的剪切、旋转等操作。
- yum -------包安装库
- 分分钟教你集成沉浸式侧滑关闭Activity
- [转]CentO下限制SSH登录次数
- C# this关键字
- 前端程序员应该知道的 15 个 jQuery 小技巧
- Redis-安装、启动
- Array.prototype鲜为人知的事实
- Spring揭秘 读书笔记 七 BeanFactory的启动分析
- python 爬虫之beautifulsoup(bs4)使用 --待完善
- 《Self-Attention Generative Adversarial Networks》里的注意力计算
- Eclipse导入hadoop源码
- fastjson转换json字符串key的首字母小写变大写的解决办法