Hashmap in java

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索引最大是整型的最大值。):

 1  /**
 2      * The default initial capacity - MUST be a power of two.
 3      */
 4     static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
 5
 6     /**
 7      * The maximum capacity, used if a higher value is implicitly specified
 8      * by either of the constructors with arguments.
 9      * MUST be a power of two <= 1<<30.
10      */
11     static final int MAXIMUM_CAPACITY = 1 << 30;
12
13     /**
14      * The bin count threshold for using a tree rather than list for a
15      * bin.  Bins are converted to trees when adding an element to a
16      * bin with at least this many nodes. The value must be greater
17      * than 2 and should be at least 8 to mesh with assumptions in
18      * tree removal about conversion back to plain bins upon
19      * shrinkage.
20      */
21     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版本源码如下:

 1  public V put(K key, V value) {
 2         return putVal(hash(key), key, value, false, true);
 3     }
 4
 5     /**
 6      * Implements Map.put and related methods
 7      *
 8      * @param hash hash for key
 9      * @param key the key
10      * @param value the value to put
11      * @param onlyIfAbsent if true, don‘t change existing value
12      * @param evict if false, the table is in creation mode.
13      * @return previous value, or null if none
14      */
15     final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
16                    boolean evict) {
17         Node<K,V>[] tab; Node<K,V> p; int n, i;   // 如果目前table为空或0,就对table初始化resize(),
18         if ((tab = table) == null || (n = tab.length) == 0)
19             n = (tab = resize()).length;     //如果当前key值的hash索引处没有值,直接在该索引处存储键值对
20         if ((p = tab[i = (n - 1) & hash]) == null)
21             tab[i] = newNode(hash, key, value, null);     //如果当前key值的hash索引处有值
22         else {
23             Node<K,V> e; K k;     //如果当前索引处的key值和待插入的key值相同(即待插入键值对与bin内第一个键值对的键值相同)----即使是不同对象,值相同就可以(因为采用了equals()方法判断)
24             if (p.hash == hash &&
25                 ((k = p.key) == key || (key != null && key.equals(k))))
26                 e = p;     //否则,如果当前bin内是以树节点的形式存储,把该节点插入到这个树中
27             else if (p instanceof TreeNode)
28                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);  //如果树中已经有这个节点,返回这个节点之前的value;如果没有返回null   //否则,即当前bin内以链表形式存储,遍历链表,插入该节点
29             else {
30                 for (int binCount = 0; ; ++binCount) {
31                     if ((e = p.next) == null) {
32                         p.next = newNode(hash, key, value, null);
33                         if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st  //如果插入节点之后,元素个数超过阈值,就把链表转换为二叉查找树
34                             treeifyBin(tab, hash);
35                         break;
36                     }
37                     if (e.hash == hash &&
38                         ((k = e.key) == key || (key != null && key.equals(k))))
39                         break;
40                     p = e;
41                 }
42             }    //如果该索引处有键值相同的键值对,那么更新原有键值对的value为新的value,返回旧的value
43             if (e != null) { // existing mapping for key
44                 V oldValue = e.value;
45                 if (!onlyIfAbsent || oldValue == null)
46                     e.value = value;
47                 afterNodeAccess(e);
48                 return oldValue;
49             }
50         }
51         ++modCount;
52         if (++size > threshold)
53             resize();
54         afterNodeInsertion(evict);
55         return null;
56     }

1.3.2 pop

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

JDK1.8版本源码如下:

 1     /**
 2      * Returns the value to which the specified key is mapped,
 3      * or {@code null} if this map contains no mapping for the key.
 4      *
 5      * <p>More formally, if this map contains a mapping from a key
 6      * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
 7      * key.equals(k))}, then this method returns {@code v}; otherwise
 8      * it returns {@code null}.  (There can be at most one such mapping.)
 9      *
10      * <p>A return value of {@code null} does not <i>necessarily</i>
11      * indicate that the map contains no mapping for the key; it‘s also
12      * possible that the map explicitly maps the key to {@code null}.
13      * The {@link #containsKey containsKey} operation may be used to
14      * distinguish these two cases.
15      *
16      * @see #put(Object, Object)
17      */
18     public V get(Object key) {
19         Node<K,V> e;
20         return (e = getNode(hash(key), key)) == null ? null : e.value;
21     }
时间: 2024-11-07 08:09:45

Hashmap in java的相关文章

hashmap in java(转)

第1部分 HashMap介绍HashMap简介HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Serializable接口.HashMap 的实现不是同步的,这意味着它不是线程安全的.它的key.value都可以为null.此外,HashMap中的映射不是有序的.HashMap 的实例有两个参数影响其性能:“初始容量” 和 “加载因子”.容量 是哈希表中桶的数量,初始容量

java.util.HashMap和java.util.HashTable (JDK1.8)【转】

一.java.util.HashMap 1.1 java.util.HashMap 综述 java.util.HashMap继承结构如下图 HashMap是非线程安全的,key和value都支持null HashMap的节点是链表,节点的equals比较的是节点的key和value内容是否相等. 1 static class Node<K,V> implements Map.Entry<K,V> { 2 final int hash; 3 final K key; 4 V valu

C++的hashmap和Java的hashmap

C++里面是这样的:typedef std::unordered_map<std::string,std::string> stringmap; std::unordered_map<std::string,double>::const_iterator got = mymap.find (input); std::pair<std::string,double> myshopping ("baking powder",0.3); myrecipe.

Summary of HashMap in Java

The tricky thing is how to decide the key for a hashmap. Especially when you intend to use self-defined objects as key. The simple rule is you have to overwrite hashcode() and equals() method. Rule 1: x.equals(y) return true if x indeed is equal to y

Java HashMap的工作原理(转载)

原文地址:http://www.importnew.com/10620.html 面试的时候经常会遇见诸如:"java中的HashMap是怎么工作的","HashMap的get和put内部的工作原理"这样的问题.本文将用一个简单的例子来解释下HashMap内部的工作原理.首先我们从一个例子开始,而不仅仅是从理论上,这样,有助于更好地理解,然后,我们来看下get和put到底是怎样工作的. 我们来看个非常简单的例子.有一个"国家"(Country)类

java根据HashMap中的值将其元素排序

思路:HashMap或Map本身没有排序功能,若要进行较轻松的排序,可利用ArrayList中的sort方法 例子: import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapSorter { public static void main(String[] args)

【Java基础】——HashMap设计原理&amp;实现分析

HashMap在Java开发中有着非常重要的角色地位,每一个Java程序员都应该了解HashMap. 本文主要从源码角度来解析HashMap的设计思路,并且详细地阐述HashMap中的几个概念,并深入探讨HashMap的内部结构和实现细节,讨论HashMap的性能问题,并且在文中贯穿着一些关于HashMap常见问题的讨论. 读完本文,你会了解到:   1. HashMap的设计思路和内部结构组成 2. HashMap中的一些概念: 什么是阀值?为什么会有阀值?什么是加载因子?它们有什么作用? 3

JAVA集合------Map (HashMap实现)

package java_util_map; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MapTest01 { public static void main(String[] args) { /*  * Map是一个接口,HashMap是Map的一个实现类  

【Simple Java】HashMap常用方法

当需要对元素进行计数时,HashMap非常有用,如下例子,统计一个字符串中每个字符出现的次数: package simplejava; import java.util.HashMap; import java.util.Map.Entry; public class Q12 { public static void main(String[] args) { HashMap<Integer, Integer> countMap = new HashMap<Integer, Intege