jdk 源码分析(2)java hashtable的结构及hashMap对比
来源:互联网 发布:最新网络歌曲下载 编辑:程序博客网 时间:2024/06/05 17:01
1)首先hashtable 只有一种存储结构。线性链表,这和hashmap 不同,hashmap 当数据量大是采用二叉树,可以加快查询。
/**
* Hashtable bucket collision list entry
*/
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;
}
2)对比put,hashtable 是线程安全的,但是valeu都不能为空,而hashmap可以的
//同步代码块,线程安全
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) { //value 不能为空
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;
}
上面的方法主要是确认里面没有相同的key。
最后调用的addEntry:这是一种头部插入数据方式。头部插入要比hashmap的尾部插入快,没有理解为什么hashmap 不采用头部插入。可能为了统一treeNode的原因吧。
private void addEntry(int hash, K key, V value, int index) {
modCount++;
Entry<?,?> tab[] = table;
if (count >= threshold) {
// Rehash the table if the threshold is exceeded
rehash();
tab = table;
hash = key.hashCode();
index = (hash & 0x7FFFFFFF) % tab.length;
}
// Creates the new entry.
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>) tab[index];
tab[index] = new Entry<>(hash, key, value, e);
count++;
}
3)get :get 简单,唯一不同就是同步
public synchronized V get(Object key) { Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { return (V)e.value; } } return null;}
4)keySet() 返回的虽然是一个set集合但是其实本身不存数据,数据还是存放在Hashtable中,包括EntrySet 等,
以前一直以为一个Set ,那么他一定存放数据了,其实错了,Set只是一堆方法。数据哪怕你放在数据库中都行,。
private class KeySet extends AbstractSet<K> { public Iterator<K> iterator() { return getIterator(KEYS); } public int size() { return count; } public boolean contains(Object o) { return containsKey(o); } public boolean remove(Object o) { return Hashtable.this.remove(o) != null; } public void clear() { Hashtable.this.clear(); }}
里面提到的hashmap可以参考:jdk 源码分析(1) hashmap的结构
总结: hashtable线程安全,但是查询速度慢,
其他东西不在分析,可以自己去看一下代码。源代码其实很简单。
阅读全文
0 0
- jdk 源码分析(2)java hashtable的结构及hashMap对比
- jdk 源码分析(1)java hashmap的结构
- JDK源码学习(1)-HashMap源码分析,HashMap与HashTable的差别
- HashMap与HashTable的对比分析
- JDK源码(一)HashSet、HashMap、Hashtable
- jdk 源码分析(17)java Semaphore 源码解析及与lock对比
- JDK源码系列(2)----HashMap源码分析
- HashMap、HashTable、TreeMap 深入分析及源码解析
- HashMap、HashTable、TreeMap 深入分析及源码解析
- HashMap及HashTable源码解析
- hash算法及Java的HashTable源码分析
- [源码解析]HashMap和HashTable的区别(源码分析解读)
- [源码解析]HashMap和HashTable的区别(源码分析解读)
- [源码解析]HashMap和HashTable的区别(源码分析解读)
- [源码解析]HashMap和HashTable的区别(源码分析解读)
- 从Java源码的角度来分析HashMap与HashTable的区别
- Java面试之从源码的角度分析Hashtable和HashMap
- JDK源码学习之HashMap (一) : 底层存储结构分析
- nltk 中的 sents 和 words
- 5.jfinal上传下载文件
- leetcode 340. Longest Substring with At Most K Distinct Characters
- Java NIO Pipe使用示例
- Java集合分类以及各自特点
- jdk 源码分析(2)java hashtable的结构及hashMap对比
- 查看本机ssh公钥,生成公钥
- Redis学习——01.redis安装
- 链表操作(链表面试题)
- Bye, sent_keys
- MySQL存储过程中的3种循环
- sql基础语句
- Python2与Python3中除法功能的异同
- Unity+Behavior Tree行为树 Behavior Designer TaskList介绍一