Java集合之HashMap

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
HashMap继承了AbstractMap,Map,Cloneable,Serializable,表示是映射,存储Key-Value,可以被克隆,可以序列化
一、常量
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 // 默认容量是16
static final int MAXIMUM_CAPACITY = 1 << 30; // 最大容量是2的30次幂
static final float DEFAULT_LOAD_FACTOR = 0.75f; // 默认负载因子是0.75
static final int UNTREEIFY_THRESHOLD = 6; // 红黑树转为链表阈值,当HashMap扩容时,如果发现链表长度小于6,则由红黑树退化为链表
static final int TREEIFY_THRESHOLD = 8; // 链表转为红黑树的阈值,在HashMap扩容时,如果发现链表长度大于8,则将链表转化为红黑树
static final int MIN_TREEIFY_CAPACITY = 64; // 当HashMap中的容量大于64时,才考虑树形化,否则桶内元素过多时,考虑的时扩容而不是树形化
二、变量
transient Node<K,V>[] table; // Node数组table,即哈希桶数组
transient int size; // 实际存储的key-value数量
transient Set<Map.Entry<K,V>> entrySet; //  待定??????
int threshold; //  HashMap扩容阈值,当size >= threshold时,HashMap就会扩容,threshold = 容量 * 负载因子,threshold越大,容纳的键值对越多
final float loadFactor; // 负载因子
transient int modCount; // 记录HashMap发生变化的次数,主要用来迭代的快速失败?????
三、存储结构
HashMap的存储结构是数组+链表+红黑树(JDK8)实现的,哈希冲突导致的链表过长时,会严重影响HashMap的性能,如查找和删除,JDK8引入红黑树,当链表过长时,将链表转化为红黑树,利用红黑树增删改查的高性能提高HashMap的性能,
四、构造函数
1.   public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }
默认负载因子是0.75,默认容量16,必须是2的幂
2.    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }
指定HashMap的容量,负载因子是默认0.75
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);
    }
指定HashMap的容量和负载因子,HashMap的最大容量MAXIMUM_CAPACITY = 1 << 30,即2的30次方
五、数组
transient Node<K,V>[] table;
HashMap有一个内部静态类:Node,实现了Map.Entry接口,本质是一个key-value,next表示是一个链表节点,用来处理哈希冲突,hash用来确定key-value保存在table数组的哪个位置。
    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;
        }
        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;
        }
    }
六、具体功能之如何根据key值计算table数组的索引
    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }
key == null的时候,key的hash值是0,key != null时,hash值是key.hashCode与key.hashCode >>>16异或的值(无符号右移,高位补0)
hash = hash(key);
i = (table.length - 1) & hash
索引等于hash按位与table.length-1,选择table.length-1这样做的好处是因为table.length总是2的N次幂,计算的到i总是在数组length范围内
七、具体功能之put方法
1.根据key计算table位置索引i
2.如果该位置table[i] == null,new Node直接保存
3.如果该位置table[i] != null,如果是Node节点,遍历链表,如果链表中没有匹配的key,则new Node插入到链表的尾部,如果链表长度大于8,将链表转为红黑树,如果链表中有匹配的key,覆盖原来的value
4.如果该位置table[i] != null,如果是TreeNode红黑树节点,则new TreeNode插入到红黑树
5.插入成功后,判断实际存在的键值对size是否大于threshold,如果大于,进行扩容
八、具体功能之get方法
public V get(Object key)
1.根据key的hash,计算位置索引i=(table.length-1) & hash
2.首先取该位置处的第一个Node first
3.如果fist是红黑树节点,则查找红黑树中匹配的key
4.如果是链表节点,则遍历链表,找到匹配的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) {
            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);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
九、具体功能之扩容
当HashMap内部不能装载更多数据时,需要扩大HashMap容量,将扩大为原来的两倍,原来的数据将根据新的hash值放在新的位置,出发条件时数据数量大于threshold
1.计算新的capacity,threshold,capitcity为原来容量的两倍,如果初始容量为0,则为16
2.创建新的数组,容量为原capacity的两倍
3.将原来数组的数据放到新的数组中
???????
原来的key-value会放在原来的位置和原来的位置+旧capacity中
十、面试问题
1.key和value可以为null吗?
key=null时,计算得到的索引为0,key为null的键值放在table[0]为头节点的链表中,value可以为null
2.key可以使用自定义类型吗
key一般使用String,Integer类型,因为这些类型时不可变的,并且实现了hashCode(),equals()方法,如果使用可变类型作为key,当key发生变化后,hashCode会发生变化,导致无法找到该键值
3.怎么实现线程安全
一是使用HashTable,但是效率很低,二是使用Collections包:Collections.synchronizeMap(hashMap);三是使用ConcurrentHashMap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值