hashtable分析
1.什么是Hash表?
Hash表又被称为散列表,是根据关键码值(key-value)也就是键值对来直接访问的一种数据结构。也就是说,它通过把关键码值映射到表中的一个位置来访问记录,用以加快查找的速度。
2.HashTable
2.1在那个包下?
来自于java.util
2.2类的继承和实现关系
Hashtable实现了一个哈希表(Map<K,V>),可以将key映射到value。任何一个非空的对象object都可以作为key或者value。
public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, Cloneable, java.io.Serializable {}

2.1如何保证成功的从hashtable中存储和查询对象
那么key必须实现hashcode方法和equals方法。
2.3影响性能的2个重要参数是什么
初始容量(initial capacity)
负载因子(load factor)
2.3.1容量
The capacity is the number of buckets in the hash table
在哈希表中容量就是桶(buckets)的数量。
初始容量(initial capacity)就是创建hashtable表时的容量
2.3.2负载因子
Generally, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the time cost to look up an entry (which is reflected in most Hashtable operations, including get and put).
通常,0.75负载因子提供了良好的时间和空间的平衡。提高负载因子的值会降低空间消耗,但是增加了时间成本去查找entry。
2.4产生Hash冲突的情况图示

图中key2和key3就产生了hash冲突,他们的地址在同一个桶上,造成一个桶上存储了2个条目。这样就知道了,如果是多个冲突的话,一个桶就有多个条目。这种情况的查找,必须按顺序进行搜索。
2.5初始容量
2.6什么是快速失败(fail-fast)
/*
The iterators returned by the iterator method of the collections returned by all of this class's "collection view methods" are fail-fast: if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Hashtable's keys and elements methods are not fail-fast.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
*/
由此类的所有“集合视图方法”返回的集合的迭代器方法返回的迭代器是快速失败的:如果在创建迭代器后的任何时间对 Hashtable 进行结构修改,除了通过迭代器自己的删除之外的任何方式方法,迭代器将抛出ConcurrentModificationException 。因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒任意的、非确定性的行为。 Hashtable 的键和元素方法返回的枚举不是快速失败的。
请注意,不能保证迭代器的快速失败行为,因为一般来说,在存在不同步的并发修改的情况下,不可能做出任何硬保证。快速失败的迭代器会尽最大努力抛出ConcurrentModificationException 。因此,编写一个依赖于这个异常的正确性的程序是错误的:迭代器的快速失败行为应该只用于检测错误。
说人话:上面的这一段是描述了hashtable的迭代器iterator在遍历一个集合的对象的时候,如果遍历的过程中对集合对象的内容进行了修改(包括增加、删除、 修改),就会抛出一个异常ConcurrentModificationException。翻译:并发修改异常。
2.7Hashtable和HashMap和ConcurrentHashMap怎么选择
As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use java.util.concurrent.ConcurrentHashMap in place of Hashtable.
从 Java 2 平台 v1.2 开始,该类被改进为实现Map接口,使其成为Java Collections Framework的成员。与新的集合实现不同, Hashtable是同步的。如果不需要线程安全的实现,建议使用HashMap代替Hashtable 。如果需要线程安全的高并发实现,则建议使用java.util.concurrent.ConcurrentHashMap代替Hashtable 。
2.8hashtable类的成员变量
private transient Entry<?,?>[] table; //定义的一个表数据结构,该结构在该类的源码中有单独定义,由2.4图也可以看出其实就是存储k-v键值的结构
private transient int count; //表中的条目总数
private int threshold; //表的阈值,是哈希表的扩容的临界条件。(该字段的值为 (int)(capacity * loadFactor)
private float loadFactor; //表的负载因子
private transient int modCount = 0; //修改的次数
2.9hashtable的4种构造方法
2.9.1指定初始容量和指定负载因子
public Hashtable(int initialCapacity, float loadFactor) {
if (initialCapacity < 0) //初始容量为0的话就抛出异常
throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor)) //负载因子小于等于0或者传入的负载因子不是一个数字就抛出异常
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
if (initialCapacity==0) //如果传入的为0,则将传入的做处理,使其等于1
initialCapacity = 1;
this.loadFactor = loadFactor; //负载因子复合条件了,直接赋值。
table = new Entry<?,?>[initialCapacity]; //创建entry表数据结构,大小是初始容量的大小
threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1); //阈值:自己看,这个很容易懂
}
指定初始容量和负载因子对其传入的值进行了一定的约束,不满足条件则直接抛出异常。
2.9.2指定初始容量和使用默认负载因子
public Hashtable(int initialCapacity) {
this(initialCapacity, 0.75f);
}
2.9.3空参构造(全部使用默认值)
hashtable的初始容量的默认值为11,负载因子为0.75f
public Hashtable() {
this(11, 0.75f);
}
2.9.4构造方法的参数是map的构造方法
public Hashtable(Map<? extends K, ? extends V> t) {
this(Math.max(2*t.size(), 11), 0.75f); //保证初始容量,使用的是默认加载因子
putAll(t);
}
2.10说说put方法是怎么实现的【重点】
//加入了synchronized锁来保证在多线程环境下的数据安全
public synchronized V put(K key, V value) {
// 确保value不为空
if (value == null) {
throw new NullPointerException();
}
// 确保key没有存在hashtable中
Entry<?,?> tab[] = table;
int hash = key.hashCode(); //获取key的哈希值,与hashmap有所不同。
int index = (hash & 0x7FFFFFFF) % tab.length; //直接取模得到目标哈希桶。
@SuppressWarnings("unchecked")
Entry<K,V> entry = (Entry<K,V>)tab[index]; //单向链表
for(; entry != null ; entry = entry.next) { //for循环查找复合条件的key,赋值为新的value,也就是覆盖掉原来的值
if ((entry.hash == hash) && entry.key.equals(key)) {
V old = entry.value;
entry.value = value;
return old;
}
}
//执行到这里就表示没有key相等的位置,那么就直接插入entry中
addEntry(hash, key, value, index);
return null;
}
//------------------------------------addEntry----------------------------------------
private void addEntry(int hash, K key, V value, int index) {
modCount++; //修改的次数进行+1
Entry<?,?> tab[] = table;
if (count >= threshold) { //如果HashTable的条目大小大于阈值,那么就会触发一次rehash()
// 超过阈值,则进行扩容
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中索引位置处的Entry赋值给e
tab[index] = new Entry<>(hash, key, value, e); //创建一个新的Entry
count++; //数量进行+1
}
总结下流程:
1.先对键值进行判断是否为null
2.通过(hash & 0x7FFFFFFF) % tab.length;来确定哈希桶的位置
3.遍历桶中的元素,如果key相等,那么则进行覆盖
4.如果不相等,那么就调用addEntry,直接进行插入
5.在addEntry方法中,先判断插入键值对后哈希表是否需要扩容,若需要则先扩容,然后重新计算哈希值;
6.最终进行插入
2.11说说get方法的实现流程【重点】
public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode(); //计算下标的位置
int index = (hash & 0x7FFFFFFF) % tab.length;
//for循环来查找符合条件的value
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
return null;
}
总的来说就是先通过key的key.hashcode()来定位一个目标桶,在通过遍历链表获取响应的元素
2.12说说remove方法【重点】
// synchronized锁保证删除成功
public synchronized V remove(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length; //index是数组的索引值
@SuppressWarnings("unchecked")
Entry<K,V> e = (Entry<K,V>)tab[index];
//遍历单向链表找,删除对应节点
for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
modCount++; //修改值++
if (prev != null) {
prev.next = e.next;
} else {
tab[index] = e.next;
}
count--;
V oldValue = e.value;
e.value = null;
return oldValue;
}
}
return null;
}
2.13说说rehash扩容方法【重点】
/*8Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently. This method is called automatically when the number of keys in the hashtable exceeds this hashtable's capacity and load factor.*/
//增加此哈希表的容量并在内部重新组织此哈希表,以便更有效地容纳和访问其条目。
//当哈希表中的键数超过此哈希表的容量和负载因子时,将自动调用此方法。
protected void rehash() {
int oldCapacity = table.length;
Entry<?,?>[] oldMap = table;
// overflow-conscious code
int newCapacity = (oldCapacity << 1) + 1; //这里是关键的扩容点
if (newCapacity - MAX_ARRAY_SIZE > 0) {
if (oldCapacity == MAX_ARRAY_SIZE)
// Keep running with MAX_ARRAY_SIZE buckets
return;
newCapacity = MAX_ARRAY_SIZE;
}
Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];
modCount++;
threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
table = newMap;
for (int i = oldCapacity ; i-- > 0 ;) {
for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
Entry<K,V> e = old;
old = old.next;
int index = (e.hash & 0x7FFFFFFF) % newCapacity;
e.next = (Entry<K,V>)newMap[index];
newMap[index] = e;
}
}
}
重点是 int newCapacity = (oldCapacity << 1) + 1; 左移一位,变为2倍,在加1,所以扩容机制是2倍+1**;
hashtable分析的更多相关文章
- Hashtable,HashMap实现原理
http://blog.csdn.net/czh0766/article/details/5260360 昨天看了算法导论对散列表的介绍,今天看了一下Hashtable, HashMap这两个类的源代 ...
- 《PHP扩展及核心》
本文地址:http://www.cnblogs.com/aiweixiao/p/8202365.html 原文地址: 欢迎关注微信公众号 程序员的文娱情怀 一.主要内容: 1️⃣php扩展的概念和底 ...
- 温习《PHP 核心技术与最佳实践》这本书
再次看这本书,顺手提炼了一下大致目录,以便后续看见目录就知道大概讲的些什么内容 PHP 核心技术与最佳实践 1.面向对象思想的核心概念 1.1 面向对象的『形』与『本』 1.2 魔术方法的应用 1.2 ...
- asp.net一些面试题(转)
基础知识 什么是面向对象 面向对象OO = 面向对象的分析OOA + 面向对象的设计OOD + 面向对象的编程OOP: 通俗的解释就是万物皆对象,把所有的事物都看作一个个可以独立的对象(单元),它们可 ...
- [源码解析]HashMap和HashTable的区别(源码分析解读)
前言: 又是一个大好的周末, 可惜今天起来有点晚, 扒开HashMap和HashTable, 看看他们到底有什么区别吧. 先来一段比较拗口的定义: Hashtable 的实例有两个参数影响其性能:初始 ...
- C#中Dictionary,Hashtable,List的比较及分析
一. Dictionary与Hashtable Dictionary与Hashtable都是.Net Framework中的字典类,能够根据键快速查找值 二者的特性大体上是相同的,有时可以把Dicti ...
- PHP源代码分析(第一章):Zend HashTable详解【转】
转载于http://www.phppan.com/2009/12/zend-hashtable/ 在PHP的Zend引擎中,有一个数据结构非常重要,它无处不在,是PHP数据存储的核心,各种常量.变量. ...
- HashTable源码分析
本次分析代码为JDK1.8中HashTable代码. HashTable不允许null作为key和value. HashTable中的方法为同步的,所以HashTable是线程安全的. E ...
- Hashtable、ConcurrentHashMap源码分析
Hashtable.ConcurrentHashMap源码分析 为什么把这两个数据结构对比分析呢,相信大家都明白.首先二者都是线程安全的,但是二者保证线程安全的方式却是不同的.废话不多说了,从源码的角 ...
- JDK 源码分析(4)—— HashMap/LinkedHashMap/Hashtable
JDK 源码分析(4)-- HashMap/LinkedHashMap/Hashtable HashMap HashMap采用的是哈希算法+链表冲突解决,table的大小永远为2次幂,因为在初始化的时 ...
随机推荐
- ServiceAccounts 及 Secrets 重大变化
关于 ServiceAccounts 及其 Secrets 的重大变化 kubernetes v1.24.0 更新之后进行创建 ServiceAccount 不会自动生成 Secret 需要对其手动创 ...
- 通过python修改本地ip
写在前面, 1 对于个人公司需要固定ip,而回家需要用到家里的ip, 2对于公司it人员,每台电脑都需要设置ip,,尤其批量的时候,这个作为it的自己知道 3运维人员,可以通过ip测试哪些ip可以用, ...
- DolphinDB +Python Airflow 高效实现数据清洗
DolphinDB 作为一款高性能时序数据库,其在实际生产环境中常有数据的清洗.装换以及加载等需求,而对于该如何结构化管理好 ETL 作业,Airflow 提供了一种很好的思路.本篇教程为生产环境中 ...
- VUE项目 启动提示 npn ERRT nissing script: dev解决办法
VUE项目 启动提示 npn ERRT nissing script: dev 提示 丢失 dev 解决办法 首先 查看项目目录里面的 package.json 文件, 文件内容如下: 发现红框这里是 ...
- Kubernetes入门实践(环境搭建)
容器技术只是解决了运维部署工作中的一个很小的问题,在现实生产环境中,除了最基本的安装,还会各式各样的需求,比如服务发现.负载均衡.状态监控.健康检查.扩容缩容.应用迁移.高可用等等.这些容器之上的管理 ...
- JDK8到JDK17有哪些吸引人的新特性?
作者:京东零售 刘一达 前言 2006年之后SUN公司决定将JDK进行开源,从此成立了OpenJDK组织进行JDK代码管理.任何人都可以获取该源码,并通过源码构建一个发行版发布到网络上.但是需要一个组 ...
- MyQR--生成个性二维码
1.二维码定义: 二维码(2-Dimensional Bar Code),是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的.它是指在一维条码的基础上扩展出另一 ...
- Claude:除ChatGPT外的另一种选择
前言 Claude 是 Anthropic 开发的人工智能产品.Anthropic 是由 11 名前 OpenAI 员工于 2022 年创立的人工智能公司,旨在构建安全.可解释和有益于人类的人工智能系 ...
- error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file o
error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file o ...
- C# 组合键判断
e.KeyboardDevice.Modifiers 同时按下了Ctrl + H键(H要最后按,因为判断了此次事件的e.Key)修饰键只能按下Ctrl,如果还同时按下了其他修饰键,则不会进入 1 pr ...