1、概述

    【hash冲突】:

        对某个元素进行哈希函数运算,得到一个地址值,要进行插入时,发现此地址被占用,称为hash冲突(哈希碰撞);

    【hash冲突解决】:

        开放定址(发生冲突,继续寻找下一块未被使用的空间)、再散列函数法、链地址法(数组+链表)

2、HashMap概述:

    1.1  3个核心:数组、单向链表、hash函数;  

    1.2  数组是HashMap的主体,链表是为了解决哈希冲突而存在的; 

    1.3  HashMap的hash表属性详解:

          1.1.1  capacity:  hash表中的桶的数量

          1.1.2  init capacity:  创建hash表时桶的数量

          1.1.3  size:  当前hash表中的数量

          1.1.4  load factor:  负载因子为0,表示空的hash表;负载因子为0.5,表示半满的hash表;

                    当hash表中的负载因子达到极限,hash表会自动扩容,称为rehashing;

                    HashMap的负载因子默认为0.75,表明当hash表3/4被填满时,会发生rehashing;

              默认的负载因子(0.75)是时间、空间上的一种折中:

                 较高的负载因子可以降低hash表占用的空间,但会增加数据查询时间;

                 较低的负载因子可以提升数据查询时间,但会增加hash表占用的内存空间;                                      

3、

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {

      +++++++内部类+++++++++
//单向链表
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next; Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
} } +++++++属性++++++++++++++
//使用数组实现
transient Node<K,V>[] table;
//初始容量16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
static final float DEFAULT_LOAD_FACTOR = 0.75f;
final float loadFactor; ++++++++构造器++++++++++++++
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
} public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
} public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
this.loadFactor = loadFactor;
this.threshold = tableSizeFor(initialCapacity);
} +++++++++++方法++++++++++++ // +++++++容量初始化
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
} //++++++hash函数
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
} //++++++put
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
} final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i; //如果数组大小为0
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length; //如果数组tab[i]位置的元素为null,直接新建结点进行存储
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null); //如果数组tab[i]位置元素不为null
else {
Node<K,V> e; K k; //如果2个hash值相同并且equals也相同,将p=tab[i];e=p;
if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))
e = p; else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
       

else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
} //+++++resize
final Node<K,V>[] resize() {
Node<K,V>[] oldTab = table;
int oldCap = (oldTab == null) ? 0 : oldTab.length;
int oldThr = threshold;
int newCap, newThr = 0;
if (oldCap > 0) {
if (oldCap >= MAXIMUM_CAPACITY) {
threshold = Integer.MAX_VALUE;
return oldTab;
}
//新容量是旧容量的2倍
else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
oldCap >= DEFAULT_INITIAL_CAPACITY)
newThr = oldThr << 1; // double threshold
}
else if (oldThr > 0) // initial capacity was placed in threshold
newCap = oldThr;
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
if (newThr == 0) {
float ft = (float)newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
(int)ft : Integer.MAX_VALUE);
}
threshold = newThr;
@SuppressWarnings({"rawtypes","unchecked"})
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
for (int j = 0; j < oldCap; ++j) {
Node<K,V> e;
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null)
newTab[e.hash & (newCap - 1)] = e;
else if (e instanceof TreeNode)
((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.next = e;
hiTail = e;
}
} while ((e = next) != null);
if (loTail != null) {
loTail.next = null;
newTab[j] = loHead;
}
if (hiTail != null) {
hiTail.next = null;
newTab[j + oldCap] = hiHead;
}
}
}
}
}
return newTab;
} }

   

4、HashMap中比较2个key是否相等的标准:

    2个key的hashCode()相同,并且2个key的equals()相同;

5、HashMap中比较2个value是否相等的标准:

    2个value的equals()相同;

6、HashMap遇到多线程并发时引发的问题???

最新文章

  1. 解决 WinXP下 libcurl.dll 无法定位程序输入点GetTickCount64问题
  2. Perl调用外部命令的方式和区别
  3. 简单的json传送数据
  4. 解决python 提示 SyntaxError: Missing parentheses in call to &#39;print&#39;
  5. DIV+CSS相对IE8的兼容问题
  6. C语言 - 结构体(struct)比特字段(:) 详细解释
  7. springboot 集成shiro
  8. Ubuntu下Tomcat初始配置
  9. Android中文API (109) —— SimpleCursorTreeAdapter
  10. Web服务,XFire的一个例子
  11. 服务器告警其一:硬盘raid问题
  12. 开发部署项目时出现:java.lang.OutOfMemoryError: PermGen space
  13. cocos2dx 动画控制概要
  14. vue的数据双向绑定和ref获取dom节点
  15. springMVC文件上传与下载(六)
  16. Python+OpenCV图像处理(七)—— 滤波与模糊操作
  17. Redis字符串操作
  18. iOS技术篇:sizeToFit 和 sizeThatFits 区别
  19. YAML 语言教程
  20. Android 6.0+ RecyclerView嵌套在ScrollView中显示不全

热门文章

  1. 9.Delegate类
  2. Mat 类的内存管理
  3. 图--生成树和最小生成树.RP
  4. a标签空的情况下 IE6 IE7下点击无效
  5. auto和register关键字
  6. JQuery UI - selectable
  7. 如何设置Oracle process值
  8. 免费证书申请——Let&#39;s Encrypt的申请与应用(IIS,Tomcat)
  9. 实现基于dotnetcore的扫一扫登录功能
  10. 不准使用xib自定义控制器view的大小