HashMap初探

HashMap (jdk8)

一、HashMap初探(jdk1.8)

1.基本结构:数组+单向链表+红黑树(1.8新增)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2Vh99fVJ-1625554456899)(E:\资料\4期\新建文件夹\image-20210626092555342.png)]

2、继承关系

/**
* 注释大致信息:
* 1、接口的基于哈希表的实现。允许null值和null键。HashMap类大致等同于Hashtable,除了它是非同步的,并且允许空值。hashMap无序
* 2、如果迭代性能很重要,则不要将初始容量设置得太高(或负载因子太低)
* 3、当哈希表中的条目数超过负载因子和当前容量的乘积时,对哈希表进行重新哈希(即重新构建内部数据结构,扩容)
* 4、默认负载因子 (.75) 在时间和空间成本之间提供了很好的权衡。较高的值会减少空间开销,但会增加查找成本(包括get和put)
* 5、线程不安全
*/
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
    // AbstractMap:继承了AbstractMap<K,V> 有一些map操作的基础方法
    // Map:实现了Map接口目的和已经被上面的 AbstractMap<K,V> 包含
    // Cloneable, Serializable接口实现:可调clone方法和可序列化
}

3、成员变量

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 默认初始容量16,手动设置时需是2的幂
static final int MAXIMUM_CAPACITY = 1 << 30; // 最大容量
static final float DEFAULT_LOAD_FACTOR = 0.75f; // 默认扩容因子
static final int TREEIFY_THRESHOLD = 8; // 链表元素大于8时,转成红黑树
static final int UNTREEIFY_THRESHOLD = 6; // 红黑树元素小于6时,转为链表
static final int MIN_TREEIFY_CAPACITY = 64; // 链表转树的另一个规则,容量大于64
transient Node<K,V>[] table; //HashMap中的数组,中间状态数据
transient Set<Map.Entry<K,V>> entrySet; // 用来存放缓存,中间状态数据; 
transient int size; // size为HashMap中K-V的实时数量(重点),注意!不是table的长度! 
transient int modCount; // 修改次数计数器
int threshold; // 扩容临界点;(capacity * loadFactor)
final float loadFactor;//负载因子 DEFAULT_LOAD_FACTOR = 0.75f赋值

// 数组和链表上存放数据的节点模型 
static class Node<K,V> implements Map.Entry<K,V> { 
    final int hash; // 扰动后的hash值
    final K key; // map的键
    V value; // map的值
    /** 只有后节点,是单向列表,因为长度不长,用单向列表可以满足操作要求,双向链表多一个前节点。 以时间换空间,时间的多余消耗可以	 *   忽略
    */
    Node<K,V> next; // 只有next节点,单向链表
    Node(int hash, K key, V value, Node<K,V> next) {
        this.hash = hash;
        this.key = key;
        this.value = value;
        this.next = next;
    }
}    

4.构造方法

1、无参构造:只初始化了 loadFactor一个参数
public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // 默认扩容因子 0.75
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V5amc44b-1625554456902)(E:\资料\4期\新建文件夹\image-20210626091124233.png)]

2、 指定初始容量和扩容因子
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;
    // 用传入的初始容量计算一个2的幂次方数值的真实初始容量,初始容量赋值给扩容临界点
    this.threshold = tableSizeFor(initialCapacity);
}
// 位运算获取一个不小于参数容量的最小的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;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qzny2j22-1625554456907)(E:\资料\4期\新建文件夹\image-20210626093737074.png)]

5、新增 map.put(k,v)

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

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)
        // 为空则进行初始化,长度改为16
        n = (tab = resize()).length;
    // 对数据进行key经过扰动后的hash寻址,找到对应槽位判断是否为空
    if ((p = tab[i = (n - 1) & hash]) == null)
        // 如果数组上这个操作正好是空的就直接把put的节点放到这个槽位
        tab[i] = newNode(hash, key, value, null);
    else {
        // 如果数组槽位不为空
        Node<K,V> e; K k;
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            // 新插入元素的key和槽位节点p的key相同,则先把p赋值给e
            e = p;
        else if (p instanceof TreeNode)
            // 若key不相同,类型是红黑树,则以红黑树的方式进行节点拼接
            e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
        else {
            // 若key不相同,类型是链表,则在链表末尾进行拼接
            for (int binCount = 0; ; ++binCount) {
                if ((e = p.next) == null) {
                    p.next = newNode(hash, key, value, null);
                    if (binCount >= TREEIFY_THRESHOLD - 1) // static final int TREEIFY_THRESHOLD = 8;
                        // 链表长度如果到达8,转红黑树
                        treeifyBin(tab, hash);
                    break;
                }
                if (e.hash == hash &&
                    // 链表上如果有相同的key,则把对应节点赋值给p
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    break;
                p = e;
            }
        }
        if (e != null) { // existing mapping for key
            // e不为null,说明put的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;
}

6、扩容

// 容量为空,则初始化容量; 否则进行2倍扩容
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)
            // 数组强制2倍扩容
            newThr = oldThr << 1; // double threshold
    }
    else if (oldThr > 0) // initial capacity was placed in threshold
        // 用于第一次进行put操作时,且new初始化时设置了阈值,首次put会把阈值赋值给容量
        newCap = oldThr; 
    else {               // zero initial threshold signifies using
        // new map时候没设置容量和阈值,此时会使用默认值
        newCap = DEFAULT_INITIAL_CAPACITY; // 16
        newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPAC // 16*0.75=12
    }
    if (newThr == 0) {
        // 如果没有设置新的阈值,则根据设置的新数组容量计算出新的阈值:0.75*newCap或者Integer.MAX_VALUE
        float ft = (float)newCap * loadFactor;
        newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM
                  (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)
                    // 如果首节点的next节点为空,直接计算节点在新数组下所处的位置放置即可
                    newTab[e.hash & (newCap - 1)] = e;
                else if (e instanceof TreeNode)
                    // 如果数组槽位连接的是红黑树,则按红黑树的方式进行分割然后拼接到新数组的槽位上
                    ((TreeNode<K,V>)e).split(this, newTab, j, oldC
                else { // preserve order
                    // 如果数组槽位连接的是链表
                    // 下面的操作,大致意思是将这个链表分为了高位链表和低位链表,是通过 e.hash & oldCap的值进行区分的
                    // 低位链表的节点位置不变,高位链表的节点元素会被移动到 newTab[j + oldCap] 位置的链表处,这样可以
                    // 降低链表的深度,而且正因为由于数组进行的2倍扩容,本来需要通过位运算计算得到的位移长度才会刚好等于
                    // [j+oldCap]的值,这也是我们要进行2倍扩容的原因,便于找到扩容后新的节点槽位,提高效率
                    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) { // 计算节点的hash是否已经超出旧数组的限制,用来分区高位和低位链表
                            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;
}

7、查询 get(Object key)

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

/**
* hash:key对应hash扰动的值
* key:映射的键
*/
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) { // (n-1)&hash 寻址key的数组
        if (first.hash == hash && // always check first node
            ((k = first.key) == key || (key != null && key.equals(k))))
            // 先检查槽位上的节点是否就是 key 是的话直接返回
            return first;
        if ((e = first.next) != null) {
            // 不是槽位上的首节点,则分为红黑树和链表再进行区分寻找key的位置
            if (first instanceof TreeNode)
                // 红黑树
                return ((TreeNode<K,V>)first).getTreeNode(hash, key);
            do {
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k))))
                    // 链表,就用e.next往下遍历寻找即可
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}

8、删除remove(Object key)

public V remove(Object key) {
    Node<K,V> e;
    return (e = removeNode(hash(key), key, null, false, true)) == null ?
        null : e.value;
}

// 流程:根据key 和 hash找到对应的node,然后将其删除,再将node的next节点移到node的位置
final Node<K,V> removeNode(int hash, Object key, Object value,
                           boolean matchValue, boolean movable) {
    Node<K,V>[] tab; Node<K,V> p; int n, index;
    // 对数组进判空,并寻址key所处的数组槽位
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (p = tab[index = (n - 1) & hash]) != null) {
        Node<K,V> node = null, e; K k; V v;
        if (p.hash == hash &&
            ((k = p.key) == key || (key != null && key.equals(k))))
            // 数组上的槽位正好就是key
            node = p;
        else if ((e = p.next) != null) {
            // key不在数组槽位,分为红黑树和链表两种情况继续寻找key的node
            if (p instanceof TreeNode)
                node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
            else {
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key ||
                         (key != null && key.equals(k)))) {
                        node = e; 
                        break;
                    }
                    p = e; // key对应节点的上一个节点,删除后需要把此节点的next节点指向删除节点对应的next
                } while ((e = e.next) != null);
            }
        }
        if (node != null && (!matchValue || (v = node.value) == value ||
                             (value != null && value.equals(v)))) {
            // 如果找到的key对应的node
            if (node instanceof TreeNode)
                ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable); // 红黑树
            else if (node == p)
                tab[index] = node.next; // key节点在数组上:直接把数组的槽位给key节点的next
            else
                p.next = node.next; //key节点在链表上:将删除节点的上一个节点的next指向删除节点的next
            ++modCount; // 操作次数++
            --size; // 键值对数量--
            afterNodeRemoval(node);
            return node; // 返回删除的node,最终被gc回收
        }
    }
    return null; // 没找到key对应的node返回null
}

9、遍历

for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
    // 1.用 map.entrySet()进行遍历; 优势:可以同时操作key和value
    Integer key = entry.getKey();
    String value = entry.getValue();
}
for (Integer integer : hashMap.keySet()) {
    // 2.map.keySet()遍历key;  很少用,速度慢
    String value =  hashMap.get(integer);
}
for (String value : hashMap.values()) {
    // 3.map.values()遍历value;  速度略快于map.entrySet()遍历,但是只能获取value
}
Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().itera
if (iterator.hasNext()) {
    // 4.map.entrySet().iterator() 迭代器; 优势:可以进行remove()操作
    Integer key = iterator.next().getKey();
    if (key.equals(1)) {
        iterator.remove();
    }
}

总结: 1,3,4视情况择优使用即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值