Hashtable源码学习
来源:互联网 发布:c语言return 1 编辑:程序博客网 时间:2024/06/10 21:31
Hashtable是线程安全的;先看其构造方法
/**
* Constructs a new, empty hashtable with a default initial capacity (11)
* and load factor (0.75).
*/
public Hashtable() {
this(11, 0.75f);
}
初始大小为11,扩容百分比为0.75;
另外研究期put方法
/**
* Maps the specified <code>key</code> to the specified
* <code>value</code> in this hashtable. Neither the key nor the
* value can be <code>null</code>. <p>
*
* The value can be retrieved by calling the <code>get</code> method
* with a key that is equal to the original key.
*
* @param key the hashtable key
* @param value the value
* @return the previous value of the specified key in this hashtable,
* or <code>null</code> if it did not have one
* @exception NullPointerException if the key or value is
* <code>null</code>
* @see Object#equals(Object)
* @see #get(Object)
*/
1.public synchronized V put(K key, V value) {
// Make sure the value is not null
2 if (value == null) {
throw new NullPointerException();
}
// Makes sure the key is not already in the hashtable.
3 Entry tab[] = table;
4 int hash = hash(key);
5 int index = (hash & 0x7FFFFFFF) % tab.length;
6 for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
7 if ((e.hash == hash) && e.key.equals(key)) {
8 V old = e.value;
9 e.value = value;
10 return old;
}
}
11 modCount++;
12 if (count >= threshold) {
// Rehash the table if the threshold is exceeded
13 rehash();
14 tab = table;
15 hash = hash(key);
16 index = (hash & 0x7FFFFFFF) % tab.length;
17 }
// Creates the new entry.
18 Entry<K,V> e = tab[index];
19 tab[index] = new Entry<>(hash, key, value, e);
20 count++;
21 return null;
}
第1行表明它的并发是通过synchronized来加锁;第2行描述value值不能为空;
第4行得到其hash值;第5到10计算put的逻辑过程;
第11-17表明当容量超过因子时,进行扩容的计算;
我的学习暂时到此
- memcached源码学习-hashtable
- memcached源码学习-hashtable
- Hashtable源码学习
- Hashtable 、ConcurrentHashMap源码学习
- JDK源码学习09----HashTable
- 集合学习--HashTable 源码初探
- stl源码剖析 详细学习笔记 hashtable
- HashMap和HashTable源码学习笔记
- HashMap、HashSet、Hashtable源码学习笔记
- Java源码集合类Hashtable学习
- java1.8 过时集合源码学习:Hashtable
- HashTable源码
- 【源码】Hashtable源码剖析
- 【源码】Hashtable源码剖析
- 深入Java集合源码学习系列:Hashtable的实现原理
- HashMap 和 HashTable 源码学习和面试总结
- 集合源码学习(十):HashTable(Java8)与HashMap比较
- JDK源码学习之集合HashMap、Hashtable、WeakHashMap比较
- Fragment详解之二——基本使用方法
- LinuxShell算术运算
- 安装sql步骤及详细说明
- 类库和框架的区别
- jackson @JsonProperty 用法
- Hashtable源码学习
- fastjson之serializer.SerializerFeature
- NoSQL的入门和概述
- HTTP 协议分析
- 《获取当前时间钟》
- LeetCode315
- linux下shell中(())双括号运算符的使用详解
- JVM内存模型
- C语言中关于pow()函数的问题