HashSet、HashMap 源码笔记

1.HashSet、HashMap总结

  • HashSet底层实现为HashMap
  • 数据结构为数组+单链表/红黑树(当数组长度不小于64,链表长度不小于8,该链表转成树结构)
  • 线程不安全
  • 不是根据插入顺序,是根据hash值排序
    备注:
    红黑树(特殊的平衡二叉树)5大特性:
    每个节点或者是黑色,或者是红色。
    根节点是黑色。
    每个叶子节点(NIL)是黑色。 [注意:这里叶子节点,是指为空(NIL或NULL)的叶子节点!]
    如果一个节点是红色的,则它的子节点必须是黑色的。
    从一个节点到该节点的子孙节点的所有路径上包含相同数目的黑节点。

2.分点详解

2.1 HashSet

2.1.1 继承实现情况

在这里插入图片描述

2.1.2 构造方法

//1.无参构造方法
public HashSet() {
        map = new HashMap<>();
    }
//不需要序列化
private transient HashMap<E,Object> map;
//2.有参构造方法,参数为集合及其子类
public HashSet(Collection<? extends E> c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        /**
        直接使用父类AbstractCollection的addAll(Collection<? extends E> c)方法
       public boolean addAll(Collection<? extends E> c) {
        	boolean modified = false;
        	for (E e : c)
         	   if (add(e))
                modified = true;
        	return modified;
    } 
    **/
    	addAll(c);
    }
//3.带有加载因子的构造方法,HashMap的默认负载因子是0.75
public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }
//4.不带负载因子的构造方法
public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }

2.1.3 添加数据

//调用HashMap的put方法
public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

2.2 HashMap

2.2.1 继承实现情况

在这里插入图片描述

2.2.2 构造方法

//初始加载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;
//1.无参构造方法
public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
//2.带初始容量参数的构造方法
public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
//map最大容量为2的30次方
static final int MAXIMUM_CAPACITY = 1 << 30;
//3.带有初始容量参数和加载因子初始方法
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);
    }
//4.数组边界值的计算方式,根据当前数组容量
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;
    }

2.2.3 扩容机制

  1. HashMap的节点为Node<K,V>,内部类
static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;
		//节点存key的hash值,key值,value值,指向下一个节点
        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;
        }

        public final boolean equals(Object o) {
            if (o == this)
                return true;
            if (o instanceof Map.Entry) {
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
                if (Objects.equals(key, e.getKey()) &&
                    Objects.equals(value, e.getValue()))
                    return true;
            }
            return false;
        }
    }
  1. 新增元素
	//1.新增元素
public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    //不需要序列化
   transient Node<K,V>[] table;
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
        	//2.第一次添加数据
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
        	//3.求出该节点在数组的索引位且该索引位数据为空
            tab[i] = newNode(hash, key, value, null);
        else {
        //4. 如果该节点所在数组索引位已存在数据
            Node<K,V> e; K k;
            //5.比较索引位节点和新增节点的hash值和key值
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                //如果一样,说明是同一个key值
                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);
                        //static final int TREEIFY_THRESHOLD = 8;定义当链表长度超过8时,数组长度超过64时,将链表树化;如果链表超过8,数组长度还没超过64,则会先扩展数组长度
                        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方法:

//初始数组长度
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

final Node<K,V>[] resize() {
        Node<K,V>[] oldTab = table;
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
        int oldThr = threshold;
        int newCap, newThr = 0;
        //如果现有数据容量大于0
        if (oldCap > 0) {
        //如果就有容量超过最大容量,数组边界值改成Integer最大值
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            //新数组长度等于旧数组长度的2倍,新数组边界值等于旧边界值的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;
            //初始扩容边界值为16*0.75,数组长度乘以加载因子
            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;

		//创建Node数组
        @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;
    }

链表树化方法 treeifyBin():

final void treeifyBin(Node<K,V>[] tab, int hash) {
        int n, index; Node<K,V> e;
        //static final int MIN_TREEIFY_CAPACITY = 64;需要满足数组长度大于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);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值