返回顶部

HashMap ( Java 8)

HashTable是早起java提供的基于hash表的实现,不允许存放null键和值,是同步的,影响开销,不太被推荐。

HashMap行为上和HashTable差不多,不是同步的,允许键和值为null,通过put(),get()来存取数据。

一、默认属性值:

这里摘出了重要属性的默认值:

// 默认容量是16,而且如果自定义容量必须上2的幂
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
//最大容量是1073741824
static final int MAXIMUM_CAPACITY = 1 << 30;
//默认负载 因子是0.75,少了可能会频繁扩容,多了可能会影响效率,默认值比较适合大多数场景
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//树化阀值为8,链表长度大于等于8就会转化成红黑树
static final int TREEIFY_THRESHOLD = 8;
//链表长度小于6就会由数退化为链表
static final int UNTREEIFY_THRESHOLD = 6;

 

二、构造方法 

HashMap 的构造方法:

    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);
    }

    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

 构造方法可以允许你自定义初始容量,但是HashMap会通过tableSizeFor(int cap)方法去讲容量转化为最接近2的幂的值

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;
    }

 

三、put方法

HashMap的PUT方法:主要调用了putVal()方法

    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

putVal()方法,标注了一些说明:

 1     final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
 2                    boolean evict) {
 3         Node<K,V>[] tab; Node<K,V> p; int n, I;
 4         // 如果table为null或者长度为0,调用resize()进行初始化为16
 5         if ((tab = table) == null || (n = tab.length) == 0)
 6             //n为当前数组的长度
 7             n = (tab = resize()).length;
 8         //将n-1和key的hash值相与值赋给i,如果当前数组第i位没有值,则将此数据插入到这个索引位置
 9         if ((p = tab[i = (n - 1) & hash]) == null)
10             tab[i] = newNode(hash, key, value, null);
11         //如果当前索引位置有数据,则新建一个节点,放在上一个节点后面    
12         else {
13             Node<K,V> e; K k;
14             //如果key相同,说明这次操作是修改操作,将val值修改
15             if (p.hash == hash &&
16                 ((k = p.key) == key || (key != null && key.equals(k))))
17                 e = p;
18             //如果p是树节点的话,则按照树节点进行插入
19             else if (p instanceof TreeNode)
20                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
21             //遍历节点,将新节点插入到链表尾部
22             else {
23                 for (int binCount = 0; ; ++binCount) {
24                     if ((e = p.next) == null) {
25                         p.next = newNode(hash, key, value, null);
26                         //如果节点数大于等于8,则进行树化操作
27                         if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
28                             treeifyBin(tab, hash);
29                         break;
30                     }
31                     //如果插入到节点和原有节点key相同,修改原有节点
32                     if (e.hash == hash &&
33                         ((k = e.key) == key || (key != null && key.equals(k))))
34                         break;
35                     p = e;
36                 }
37             }
38             //e不为空说明是一次修改操作,将当前节点e的value替换为新的
39             if (e != null) { // existing mapping for key
40                 V oldValue = e.value;
41                 if (!onlyIfAbsent || oldValue == null)
42                     e.value = value;
43                 afterNodeAccess(e);
44                 return oldValue;
45             }
46         }
47         ++modCount;
48         //如果数组大小大于阀值,则扩容
49         if (++size > threshold)
50             resize();
51         afterNodeInsertion(evict);
52         return null;
53     }                                                                                                                        

 

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;
            }
             //扩容后未达到极限,新的数组扩容为两倍,阈值也扩至原来两倍
            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;
         //默认的容量16,阈值0.75*16=12
        else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        //如果阈值为0(自定义数组大小)设置阈值为负载因子*容量
        if (newThr == 0) {
            float ft = (float)newCap * loadFactor;
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
                      (int)ft : Integer.MAX_VALUE);
        }
         //阈值为新计算的值
         threshold = newThr;
         // ···
         return newTab;
     }              

treeifyBin()方法:

    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);
        }
    }    

 

 

Clear()方法:将数组内清空

public void clear() {
        Node<K,V>[] tab;
        modCount++;
        if ((tab = table) != null && size > 0) {
            size = 0;
            for (int i = 0; i < tab.length; ++i)
                tab[i] = null;
        }
    }

 

get()方法,调用getNode()方法:

    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }

    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        //数组不为空
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            //检查第一个节点的值是否对的上
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
            //第一个值对不上,看看有没有下一个节点
            if ((e = first.next) != null) {
                //如果第一个节点上树节点,则去树中查找
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                //如果是链表,则遍历全部链表,看是否有该key值
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        //没有返回null
        return null;
    }                    

containsKey():同样是去调用getNode()

    public boolean containsKey(Object key) {
        return getNode(hash(key), key) != null;
    }

 

posted @ 2020-01-30 11:56  _懒惰的猫  阅读(193)  评论(0编辑  收藏  举报