Android 开发也要懂得数据结构 - HashMap源码

Posted 进击的包籽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 开发也要懂得数据结构 - HashMap源码相关的知识,希望对你有一定的参考价值。

文章目录

  • HashMap不仅是android开发中常用的数据结构,面试也是高频出现,所以了解一下源码还是非常必要的。
  • 本文章使用的是 JDK1.8 ,不同版本源码有差异。
  • 文章里面的图片来自 极客时间,王争老师的数据结构与算法课
  • 极客时间 - 数据结构与算法

1.HashMap特点

  • Collection 是集合,有数组(ArrayList)查找快增删慢,有链表(LinkList)增删快查找慢,Map 就是数组与链表的结合体,结合了两的优点。
  • HashMap 的数据关系是 keyvalue 的映射关系,key 是唯一的,value 是可以重复的。
  • HashMapHash , 是因为 key 是需要计算哈希值,这种数组就是散列表
  • HashMap 可以理解为 key计算后的位置用 数组 保存,数组里面的内容放着 链表 ,链表的节点是 key-value 一个个保存起来,查找的时候,快速找到数组中对应的位置,然后遍历链表。
  • HashMap 非线程安全,可以用 HashTable
  • HashMap 数据是无序的,需要有序的使用 LinkedHashMap

2.HashMap 的继承关系

  • HashMap继承于 Map,而LinkedHashMap 是继承于HashMap

3.HashMap常用方法

3.1 构造方法

  • 默认构造方法,只初始化了扩容因数,0.75,就是HashMap的数组容量使用了75%,就要进行扩容操作了,注释还说明初始容量为16
    /**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public HashMap() 
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    
  • 自定义初始容量和扩容因子的构造方法,initialCapacity为初始容量,最大不超过2的30次方,loadFactor为扩容因子,必需为大于0的浮点数。
    /**
     * 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;

    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public HashMap(int initialCapacity, float loadFactor) 
        //小于0走异常处理。
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        //最大不能超过1左移30位,也就是2的30次方,非常大的数。  
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        //如果扩容因子小于0,或者不是浮点数,报异常处理。
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    
  • 传入Map的构造方法,默认扩容因子也是0.75,可以将Map转化为HashMap。
    /**
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
     * default load factor (0.75) and an initial capacity sufficient to
     * hold the mappings in the specified <tt>Map</tt>.
     *
     * @param   m the map whose mappings are to be placed in this map
     * @throws  NullPointerException if the specified map is null
     */
    public HashMap(Map<? extends K, ? extends V> m) 
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        putMapEntries(m, false);
    

3.2 放入元素 put(K key, V value)

  • 首先看 hash(Object key) 方法,就是判断元素存放在数组的位置,如果空就返回 0,否则 key 的哈希值用临时变量 h 保存,再和 h 无符号右移16位的结果(>>>是无符号右移,高位补0),做异或操作(^是异或),算出存放在数组的位置,这种哈希计算过的数组其实就是 散列表。如果计算出来的结果一样,也就是哈希碰撞,那数据后面会用链表存放。
  • 下图就是散列表。

  • putVal(hash(key), key, value, false, true),方法有五个参数,第一个是计算的位置,第二个是 key , 第三个是value , 第四个是否修改已存在的值,最后一个参数看意思是创建表格。
  • 如果key 为空,null 是无法计算哈希值的,就返回0,所以 HashMap 是可以放一个 key 为空的元素的。
    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) 
        return putVal(hash(key), key, value, false, true);
    

    /**
     * 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;
        //如果key为空,就返回0
        //否者key的哈希码 异或 key无符号右移16位的结果
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    
  • Node<K,V> 节点类,我们可以看到,是单链表结构,还重写了equals(Object o)
    /**
     * Basic hash bin node, used for most entries.  (See below for
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
     */
    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;
        

        public final K getKey()         return key; 
        public final V getValue()       return value; 
        public final String toString()  return key + "=" + value; 

        public final int hashCode() 
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        

        public final V setValue(V newValue) 
            V oldValue = value;
            value = newValue;
            return oldValue;
        

		//重写equals
        public final boolean equals(Object o) 
            if (o == this)
                return true;
            if (o instanceof Map.Entry) 
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                //对比key和value
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            
            return false;
        
    

3.3 HashMap核心 putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)

  • 这个方法看到Node<K,V>[],HashMap的结构就能理解了吧。

  • HashMap为了解决散列冲突,就用了链表法。

  • 存放数据的链表,在长度在 8以下 的时候,是 链表 存储,8以上 或者数组长度大于64时就红黑树存储。

  • 由于方法细节太多,直接写注释一步步好理解。

   /**
     * 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;

    /**
     * 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;
        //如果散列表为空或者散列表长度为0
        if ((tab = table) == null || (n = tab.length) == 0)
        	//resize()是创建哈希表,长度为16,并且将长度赋值给n
            n = (tab = resize()).length;
        //找到hash值在当前哈希表中的位置,该位置的节点赋值给p,且判断该位置是否为空
        if ((p = tab[i = (n - 1) & hash]) == null)
        	//如果为空就把这个元素放在这个位置
            tab[i] = newNode(hash, key, value, null);
        else 
            Node<K,V> e; K k;
            //如果hash值相同,key也相同
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                //将节点p赋值给e              
                e = p;
            //如果p是树节点
            else if (p instanceof TreeNode)
            	//创建一个树节点赋值给e
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else 
            	//链表节点,就遍历链表
                for (int binCount = 0; ; ++binCount) 
                	//将p的下一节点赋值给e,且为空
                    if ((e = p.next) == null) 
                    	//找到链表尾部,插入新的节点
                        p.next = newNode(hash, key, value, null);
                        //如果链表的长度大于8的时候
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            //链表转树结构
                            treeifyBin(tab, hash);
                        break;
                    
                    //遍历到的位置已经有元素了,这里就是遍历链表一直循环next,直到为空停止
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                
            
            //如果当前节点不为空,前面的操作除了最后一个else,其他就是找到已存在的节点
            if (e != null)  // existing mapping for key
            	//当前节点的值赋值给oldValue 
                V oldValue = e.value;
                //如果不修改值,或者oldValue 为空(存储value为空)
                if (!onlyIfAbsent || oldValue == null)
                	//就修改当前节点的值
                    e.value = value;
                afterNodeAccess(e);
                //修改值,在这return,不再增加数据的长度
                return oldValue;
            
        
        ++modCount;
        //添加好元素,长度+1
        if (++size > threshold)
            //扩容操作
            resize();
        afterNodeInsertion(evict);
        return null;
    
    
    /**
     * Replaces all linked nodes in bin at index for given hash unless
     * table is too small, in which case resizes instead.
     */
    final void treeifyBin(Node<K,V>[] tab, int hash) 
        int n, index; Node<K,V> e;
        //如果哈希表为空,或者哈希表长度小于64,优先扩容,而不是转为红黑树
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
            //扩容
            resize();
        //否则判断不为空就转为树节点
        else if ((e = tab[index = (n - 1) & hash]) != null) 
            TreeNode<K,V> hd = null, tl = null;
            do 
                TreeNode<K,V> p = replacementTreeNode(e, null);
                if (tl == null)
                    hd = p;
                else 
                    p.prev = tl;
                    tl.next = p;
                
                tl = p;
             while ((e = e.next) != null);
            if ((tab[index] = hd) != null)
                hd.treeify(tab);
        
    

3.4 扩容 resize()

  • 扩容为原来的2倍大小,扩容完,需要重写计算位置,重写排位置。
    /**
     * Initializes or doubles table size.  If null, allocates in
     * accord with initial capacity target held in field threshold.
     * Otherwise, because we are using power-of-two expansion, the
     * elements from each bin must either stay at same index, or move
     * with a power of two offset in the new table.
     *
     * @return the table
     */
    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) 
        	//最大不能超过Integer.MAX_VALUE
            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
            //创建默认大小,长度16
            newCap = DEFAULT_INITIAL_CAPACITY;
            //阈值0.75 * 16
            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;
    

3.5 查找元素 get(Object key)

  • 查找比较简单,key为空,就是hash为0,有就返回,没有就返回null。
  • key 不为空,找到就返回 value,找不到就返回null。
    /**
     * 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)
     */
    Android 开发也要懂得数据结构 - ArrayList源码

Android 开发也要懂得数据结构 - LinkList源码

学PHP也要懂得HTML

深入懂得android view 生命周期

Android 开发也要掌握的Java知识 -ThreadLocal

Android 面试之必问性能优化