1. HashMap要点:

1.1 基本数据结构: 

采用 数组+链表/平衡二叉查找树 的组合形式,所有键值对都以Entry<K,V>形式存储(每put进一个键值对,就会实例化一个Entry<K, V>)。

  • 数组:Entry<K,V>数组,以hash( key.hashCode() ) 为数组索引。即计算键值的hash值,以此为索引存储键值对Entry<K, V>。数组长度总是2的n次方(这与hash有关,后边会讲)
  • 链表:如果hash()方法算出的hash值相同,在该索引处,建立链表,存储hash值相同的键值对。
  • 平衡二叉查找树:如果该当前索引处是一个链表,且插入当前键值对后,该链表处元素个数超过阈值(8),则把当前索引处的链表转换为一个平衡二叉查找树;如果当前索引处是一个树,则直接直接插入当前键值对

JDK1.8版本中table的定义(Node<K, V>实现了接口Entry<K,V>):

    /**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;  //static class Node<K,V> implements Map.Entry<K,V>

table初值为16,扩充时,每次扩原来的两倍。下边是对table初值的定义(java中基本数据类型的字长是固定的,平台独立。如int总是4bytes的,因此table索引最大是整型的最大值。):

  /**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 /**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30; /**
* The bin count threshold for using a tree rather than list for a
* bin. Bins are converted to trees when adding an element to a
* bin with at least this many nodes. The value must be greater
* than 2 and should be at least 8 to mesh with assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;  //每个索引处链表元素个数的阈值,超过则用二叉查找树存

1.2 哈希算法

JDK1.8版本的源码如下:

    /**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

即将key.hashCode()的高16位与低16位按位异或的结果作为键值的hash值。

有几点:

  1. key.hashCode():hashCode()是Object类的本地方法。键值对象类可以重写自己的hashCode()方法,例如Integer类的hashCode()方法仅返回其int值。
  2. hash():为避免key.hashCode()获得的hash值重复太多,如Integer类的就很简单,所以就又加了一层hash。但是也可能原来的hashCode()方法就已经很好,所以衡量效率和效果之后,采用尽量简单的方法来获取新的hash值。
  3. h>>>16:这里hash值的结果是将高16位与低16位异或,并存储在低16位中。即hash值是16位bit值,这与1.1中介绍的table长度是相关的,因为table索引就是hash值。

1.3 主要基本操作

1.3.1 put()

  • 索引处没有键值对,直接插入为链表
  • 否则,如果树中或链表中有相同key值,则更新原来的value值,并返回旧value值
  • 否则,如果是链表则插入链表,插入后超过长度阈值,则转为二叉查找树;如果是树,则插入树

JDK1.8版本源码如下:

  public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
} /**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
   // 如果目前table为空或0,就对table初始化resize(),
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//如果当前key值的hash索引处没有值,直接在该索引处存储键值对
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
//如果当前key值的hash索引处有值
else {
Node<K,V> e; K k;
//如果当前索引处的key值和待插入的key值相同(即待插入键值对与bin内第一个键值对的键值相同)----即使是不同对象,值相同就可以(因为采用了equals()方法判断)
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//否则,如果当前bin内是以树节点的形式存储,把该节点插入到这个树中
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);  //如果树中已经有这个节点,返回这个节点之前的value;如果没有返回null
   //否则,即当前bin内以链表形式存储,遍历链表,插入该节点
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;
}
}
    //如果该索引处有键值相同的键值对,那么更新原有键值对的value为新的value,返回旧的value
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;
}

1.3.2 pop

  1. 如果hashmap中有当前的key,返回其对应的value,否则返回null

JDK1.8版本源码如下:

     /**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* <p>A return value of {@code null} does not <i>necessarily</i>
* indicate that the map contains no mapping for the key; it's also
* possible that the map explicitly maps the key to {@code null}.
* The {@link #containsKey containsKey} operation may be used to
* distinguish these two cases.
*
* @see #put(Object, Object)
*/
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}

Hashmap in java的更多相关文章

  1. java.util.HashMap和java.util.HashTable (JDK1.8)

    一.java.util.HashMap 1.1 java.util.HashMap 综述 java.util.HashMap继承结构如下图 HashMap是非线程安全的,key和value都支持nul ...

  2. LeetCode算法题-Design HashMap(Java实现)

    这是悦乐书的第299次更新,第318篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第167题(顺位题号是706).在不使用任何内置哈希表库的情况下设计HashMap.具体 ...

  3. C++的hashmap和Java的hashmap

    C++里面是这样的:typedef std::unordered_map<std::string,std::string> stringmap; std::unordered_map< ...

  4. Keys of HashMap in Java

    The tricky thing is how to decide the key for a hashmap. Especially when you intend to use self-defi ...

  5. java——HashMap的实现原理,自己实现简单的HashMap

    数据结构中有数组和链表来实现对数据的存储,但是数组存储区间是连续的,寻址容易,插入和删除困难:而链表的空间是离散的,因此寻址困难,插入和删除容易. 因此,综合了二者的优势,我们可以设计一种数据结构-- ...

  6. JAVA源码分析-HashMap源码分析(一)

    一直以来,HashMap就是Java面试过程中的常客,不管是刚毕业的,还是工作了好多年的同学,在Java面试过程中,经常会被问到HashMap相关的一些问题,而且每次面试都被问到一些自己平时没有注意的 ...

  7. java--HashMap多线程并发问题分析

    并发问题的症状 多线程put后可能导致get死循环 从前我们的Java代码因为一些原因使用了HashMap这个东西,但是当时的程序是单线程的,一切都没有问题.后来,我们的程序性能有问题,所以需要变成多 ...

  8. Java学习——HashMap

    遍历 Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map. ...

  9. 转发 java数据结构之hashMap详解

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

随机推荐

  1. 51nod 棋盘问题(博弈论)

    题目链接: 棋盘问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在玩一个游戏,每次寻找边长为x的正方 ...

  2. MarkDown认识与入门

    Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...

  3. 基于JQuery的渐隐渐现轮播

    <div id="ads"> <div> <!--轮播图片--> <ul> <li><a href="# ...

  4. CSS标签知识

    一.内联标签和块标签的区别 内联,内嵌,行内属性标签: 1.默认同行可以继续跟同类型标签: 2.内容撑开宽度 3.不支持宽高 4.不支持上下的margin和padding 5.代码换行被解析 块属性标 ...

  5. 出现,视图必须派生自 WebViewPage 或 WebViewPage错误解决方法

    遇到这种问题是因为我新建了Areas,在Areas里面建Controllers,Models,Views.所以在View文件夹下面湿没有Web.config文件的. 解决方法:(复制views中的we ...

  6. 4种处理excel文件的技术

    1.OLE Automation:处理excel文件会启动一个excel的进程,程序和excel进程通信来处理excel文件,这种方式占用服务器资源,不适合于网站的开发. 2.把Excel当成数据库, ...

  7. 四.CSS声明

    概述 一条CSS规则包括选择符和声明两个部分,声明又包括属性和值,属性指出要影响样式的那个方面,而值用于把属性设定成什么 每个HTML元素有很多属性,可以用CSS来设定,但值却只有少数的几种 CSS属 ...

  8. 人情世故&潜规则

    大凡成功的牛人,无一例外都明白这一点.他们读懂了社会的本质和人际交往的潜规则,知道对方需要什么,知道对方脑子里在想什么.你几乎看不见他奔波劳碌,但是在不动声色中,他就已经实现人生目标.他们成功的密码是 ...

  9. 日志备份和差异备份还原中的常见问题示例(转自&邹建)

    --创建测试 CREATE DATABASE db GO    --正常备份 BACKUP DATABASE db TO DISK='c:\1.bak' WITH FORMAT BACKUP LOG  ...

  10. ASP.NET 窗体间传值实现方法详解

    假设ParentForm.aspx 页面上有TextBox1文本框和Open按钮点击Open按钮弹出SubForm.aspx,SubForm.aspx页面上有TextBox1文本框和Close按钮点击 ...