【Java集合框架库】HashMap类

HashMap类

HashMap类是基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。它继承于AbstractMap类,实现了Map、Cloneable、java.io.Serializable接口。

HashMap有两个参数影响其性能:初始容量和加载因子,初始容量是哈希表在创建时的容量,默认为16个大小。加载因子默认为0.75,当哈希表中的节点个数超过加载因子*当前节点个数时,需要进行2倍扩容操作。

(除了不同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同)。但是,此类不保证映射的顺序,特别是它不保证该顺序恒久不变。另外,HashMap是非线程安全的,也就是说在多线程的环境下,可能会存在问题,而Hashtable是线程安全的。

附上一个我认为总结比较完善的博客链接:HashMap

HashMap的特点
public class HashMap<K,V>
    extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable
    
 static class Entry<K,V> implements Map.Entry<K,V> 
 
 private abstract class HashIterator<E> implements Iterator<E>

从继承的接口来看,有如下特点:

  • 1、Map:是存放一对值的最大接口!即接口中的每个元素都是一对, 以key-value的形式保存,并且Key是不重复的,元素的存储位置由key决定。也就是可以通过key去寻找key-value的位置,从而得到value的值。适合做查找工作。
  • 2、Cloneable:Cloneable是标记型的接口,它们内部都没有方法和属性,实现 Cloneable来表示该对象能被克隆,能使用Object.clone()方法。如果没有实现 Cloneable的类对象调用clone()就会抛出CloneNotSupportedException。
  • 3、Serializable:public interface Serializable类通过实现 java.io.Serializable 接口以启用其序列化功能。
  • 4、Iterator:可以使用迭代器遍历。
HashMap的存储结构

HashMap的主干是一个Entry数组。Entry是HashMap的基本组成单元,每一个Entry包含一个key-value键值对。(其实所谓Map其实就是保存了两个对象之间的映射关系的一种集合)

//HashMap的主干数组,可以看到就是一个Entry数组,初始值为空数组{},主干数组的长度一定是2的次幂。
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;

Entry是HashMap中的一个静态内部类。代码如下:

static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;//存储指向下一个Entry的引用,单链表结构
        int hash;//对key的hashcode值进行hash运算后得到的值,存储在Entry,避免重复计算
        
        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        } 
 }

我们都知道HashMap是通过key值进行哈希算法从而计算其所存储的位置的,但是,使用这种方法计算必定会引起哈希冲突。在HashMap中为了减少哈希冲突所带来的影响,HashMap采用了数组+链表形式的存储结构。如图所示:

在这里插入图片描述

注:为了解决数组槽位上所链接的数据过多(即拉链过长的情况)导致性能下降的问题,JDK1.8在JDK1.7的基础上增加了红黑树来进行优化。即当链表超过8时,链表就转换为红黑树,利用红黑树快速增删改查的特点提高HashMap的性能,其中会用到红黑树的插入、删除、查找等算法。

其中数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前entry的next指向null),那么查找,添加等操作很快,仅需一次寻址即可;如果定位到的数组包含链表,对于添加操作,其时间复杂度为O(n),首先遍历链表,存在即覆盖,否则新增;对于查找操作来讲,仍需遍历链表,然后通过key对象的equals方法逐一比对查找。所以,性能考虑,HashMap中的链表出现越少,性能才会越好。

HashMap的基本使用
public class HashMapTest {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        /**
         * 插入方法
         */
        map.put("Tom", 12);
        map.put("Jack", 15);
        map.put("Jim", 20);
        map.put("Tom", 65); //HashMap中不允许插入重复key值,若插入重复key值,会使用新value值替换已经存在的value值
        map.put(null, 100);

        /**
         * 删除方法
         */
        map.remove("Jack");

        /**
         * 更改方法
         */
        map.replace("Jim", 200);

        /**
         * 遍历方法
         */
        //使用迭代器遍历:因为Map接口与List接口不属于同一集合下,因此Map接口下的集合不能使用ListIterator进行逆向遍历
        System.out.println("遍历方法一:使用迭代器正向遍历key值和value值");
        //因为HashMap底层数据结构采用的是数组+链表形式,key-value值存储在链式结点中,
        //因此需要先获取结点对象再获取迭代对象
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            System.out.println("key值:" + next.getKey() + "   value值:" + next.getValue());
        }
        System.out.println("===========================================================");
        System.out.println("遍历方法二:使用foreach方法遍历key值和value值");
        for (Map.Entry<String, Integer> next : map.entrySet()) {
            System.out.println("key值:" + next.getKey() + "   value值:" + next.getValue());
        }
        System.out.println("===========================================================");
        //遍历单一值:单一遍历key值或者单一遍历value值
        System.out.println("使用迭代器单一遍历key值");
        Iterator<String> iterator_key = map.keySet().iterator();
        while (iterator_key.hasNext()) {
            String key = iterator_key.next();
            System.out.println("key值:" + key);
        }
        System.out.println("===========================================================");
        //遍历单一值:单一遍历key值或者单一遍历value值
        System.out.println("使用迭代器单一遍历value值");
        Iterator<Integer> iterator_value = map.values().iterator();
        while (iterator_value.hasNext()) {
            Integer value = iterator_value.next();
            System.out.println("value值:" + value);
        }
        System.out.println("===========================================================");
        //遍历单一值:单一遍历key值或者单一遍历value值
        System.out.println("使用foreach方法单一遍历key值");
        for (String key : map.keySet()) {
            System.out.println("key值:" + key);
        }
        System.out.println("===========================================================");
        //遍历单一值:单一遍历key值或者单一遍历value值
        System.out.println("使用foreach方法单一遍历value值");
        for (Integer value : map.values()) {
            System.out.println("value值:" + value);
        }
    }
}

运行结果:

在这里插入图片描述

HashMap的源码分析
1、主要的成员变量
/**实际存储的key-value键值对的个数*/
transient int size;

/**阈值,当table == {}时,该值为初始容量(初始容量默认为16);当table被填充了,也就是为table分配内存空间后,
threshold一般为 capacity*loadFactory。HashMap在进行扩容时需要参考threshold,后面会详细谈到*/
int threshold;

/**负载因子,代表了table的填充度有多少,默认是0.75
加载因子存在的原因,还是因为减缓哈希冲突,如果初始桶为16,等到满16个元素才扩容,某些桶里可能就有不止一个元素了。
所以加载因子默认为0.75,也就是说大小为16的HashMap,到了第13个元素,就会扩容成32。
*/
final float loadFactor;

/**HashMap被改变的次数,由于HashMap非线程安全,在对HashMap进行迭代时,
如果期间其他线程的参与导致HashMap的结构发生变化了(比如put,remove等操作),
需要抛出异常ConcurrentModificationException*/
transient int modCount;
2、构造函数

当采用无参构造时,HashMap会先将table数组赋值为空数组,待第一次添加元素时,会使用默认构造值:initialCapacity默认为16,loadFactory默认为0.75。

/**
 * 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) {
 //此处对传入的初始容量进行校验,最大不能超过MAXIMUM_CAPACITY = 1<<30(230)
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal initial capacity: " +
                                           initialCapacity);
 //初始容量最大不能超过2的30次方
    if (initialCapacity > MAXIMUM_CAPACITY)
        initialCapacity = MAXIMUM_CAPACITY;
 //显然加载因子不能为负数  || 判断是不是一个数字
    if (loadFactor <= 0 || Float.isNaN(loadFactor))
        throw new IllegalArgumentException("Illegal load factor: " +
                                           loadFactor);
    
    this.loadFactor = loadFactor;
    threshold = initialCapacity;
    init(); //init方法在HashMap中没有实际实现,不过在其子类如 linkedHashMap中就会有对应实现
}
3、添加函数
public V put(K key, V value) {
        //如果table数组为空数组{},进行数组填充(为table分配实际内存空间),入参为threshold,
        //此时threshold为initialCapacity 默认是1<<4(24=16)
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        //如果key为null,存储位置为table[0]或table[0]的冲突链上
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key); //对key的hashcode进一步计算,确保散列均匀
        int i = indexFor(hash, table.length); //获取在table中的实际位置
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            //如果该对应数据已存在,执行覆盖操作。用新value替换旧value,并返回旧value
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }
        modCount++; //保证并发访问时,若HashMap内部结构发生变化,快速响应失败
        addEntry(hash, key, value, i); //新增一个entry
        return null;
    }
  • inflateTable这个方法用于为主干数组table在内存中分配存储空间,通过roundUpToPowerOf2(toSize)可以确保capacity为大于或等于toSize的最接近toSize的二次幂,比如toSize=13,则capacity=16;to_size=16,capacity=16;to_size=17,capacity=32。
private void inflateTable(int toSize) {
        int capacity = roundUpToPowerOf2(toSize);//capacity一定是2的次幂
        /**此处为threshold赋值,取capacity*loadFactor和MAXIMUM_CAPACITY+1的最小值,
        capaticy一定不会超过MAXIMUM_CAPACITY,除非loadFactor大于1 */
        threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
        table = new Entry[capacity];
        initHashSeedAsNeeded(capacity);
    }
  • roundUpToPowerOf2中的这段处理使得数组长度一定为2的次幂,Integer.highestOneBit是用来获取最左边的bit(其他bit位为0)所代表的数值。
private static int roundUpToPowerOf2(int number) {
    // assert number >= 0 : "number must be non-negative";
    return number >= MAXIMUM_CAPACITY
            ? MAXIMUM_CAPACITY
            : (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
  • hash函数
/**这是一个神奇的函数,用了很多的异或,移位等运算
对key的hashcode进一步进行计算以及二进制位的调整等来保证最终获取的存储位置尽量分布均匀*/
final int hash(Object k) {
        int h = hashSeed;
        if (0 != h && k instanceof String) {
            return sun.misc.Hashing.stringHash32((String) k);
        }
        
        h ^= k.hashCode();
        
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
  • 以上hash函数计算出的值,通过indexFor进一步处理来获取实际的存储位置。
/**
 * 返回数组下标
 */
static int indexFor(int h, int length) {
    return h & (length-1);
}
  • h &(length-1)保证获取的index一定在数组范围内,举个例子,默认容量16,length-1=15,h=18,转换成二进制计算为index=2。位运算对计算机来说,性能更高一些(HashMap中有大量位运算)。
  • 所以最终存储位置的确定流程是这样的:

在这里插入图片描述

  • 再来看看addEntry的实现:
void addEntry(int hash, K key, V value, int bucketIndex) {
        if ((size >= threshold) && (null != table[bucketIndex])) {
            resize(2 * table.length); //当size超过临界阈值threshold,并且即将发生哈希冲突时进行扩容
            hash = (null != key) ? hash(key) : 0;
            bucketIndex = indexFor(hash, table.length);
        }
        
        createEntry(hash, key, value, bucketIndex);
    }
  • 通过以上代码能够得知,当发生哈希冲突并且size大于阈值的时候,需要进行数组扩容,扩容时,需要新建一个长度为之前数组2倍的新的数组,然后将当前的Entry数组中的元素全部传输过去,扩容后的新数组长度为之前的2倍,所以扩容相对来说是个耗资源的操作。
  • 我们来继续看上面提到的resize方法:
void resize(int newCapacity) {
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }
        
        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable, initHashSeedAsNeeded(newCapacity));
        table = newTable;
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }
  • 如果数组进行扩容,数组长度发生变化,而存储位置 index = h & (length - 1),index也可能会发生变化,需要重新计算index。
void transfer(Entry[] newTable, boolean rehash) {
        int newCapacity = newTable.length;
     //for循环中的代码,逐个遍历链表,重新计算索引位置,将老数组数据复制到新数组中去(数组不存储实际数据,所以仅仅是拷贝引用而已)
        for (Entry<K,V> e : table) {
            while(null != e) {
                Entry<K,V> next = e.next;
                if (rehash) {
                    e.hash = null == e.key ? 0 : hash(e.key);
                }
                int i = indexFor(e.hash, newCapacity);
                //将当前entry的next链指向新的索引位置,newTable[i]有可能为空,有可能也是个entry链,如果是entry链,直接在链表头部插入。
                e.next = newTable[i];
                newTable[i] = e;
                e = next;
            }
        }
    }
  • 而至于HashMap为何使用2倍扩容 我们可以看到上面的indexFor()方法是将通过key值获取到的hash值转化为可以存储的下标值index,而其所使用的方法为return h & (length-1);其实针对转化hash值为index值的方法还有一种十分简单的:return h % length;这两种方法在处理的数值length2的幂次方时所得到的结果是相同的,但是按位运算的效率要大于普通的算数运算,这也是为什么HashMap采用第一种方法的原因。因此我们也同样可以看出为什么HashMap的初始容量为16;以及为什么HashMap的扩容方法为2倍扩容。目的就是为了保证数组大小始终为2的幂次方,从而保证使用位运算计算index值时的正确性。当我们传入指定参数进行构造table的大小时,HashMap会调用private static int roundUpToPowerOf2(int number)方法来确保参数大小始终为2的幂次方
4、get方法
 public V get(Object key) {
     //如果key为null,则直接去table[0]处去检索即可。
        if (key == null)
            return getForNullKey();
        Entry<K,V> entry = getEntry(key);
        return null == entry ? null : entry.getValue();
 }
  • get方法通过key值返回对应value,如果key为null,直接去table[0]处检索。我们再看一下getEntry这个方法。
final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }
        //通过key的hashcode值计算hash值
        int hash = (key == null) ? 0 : hash(key);
        //indexFor (hash&length-1) 获取最终数组索引,然后遍历链表,通过equals方法比对找出对应记录
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash && 
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }

可以看出,get方法的实现相对简单,key(hashcode)–>hash–>indexFor–>最终索引位置,找到对应位置table[i],再查看是否有链表,遍历链表,通过key的equals方法比对查找对应的记录。要注意的是,有人觉得上面在定位到数组位置之后然后遍历链表的时候,e.hash == hash这个判断没必要,仅通过equals判断就可以。其实不然,试想一下,如果传入的key对象重写了equals方法却没有重写hashCode,而恰巧此对象定位到这个数组位置,如果仅仅用equals判断可能是相等的,但其hashCode和当前对象不一致,这种情况,根据Object的hashCode的约定,不能返回当前对象,而应该返回null。

5、删除函数
/**
     * Removes the mapping for the specified key from this map if present.
     *
     * @param  key key whose mapping is to be removed from the map
     * @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 remove(Object key) {
        Entry<K,V> e = removeEntryForKey(key);
        return (e == null ? null : e.value);
    }
/**
 * Removes and returns the entry associated with the specified key
 * in the HashMap.  Returns null if the HashMap contains no mapping
 * for this key.
 */
final Entry<K,V> removeEntryForKey(Object key) {
    if (size == 0) {
        return null;
    }
    int hash = (key == null) ? 0 : hash(key);
    int i = indexFor(hash, table.length);
    Entry<K,V> prev = table[i];
    Entry<K,V> e = prev;
    
    while (e != null) {
        Entry<K,V> next = e.next;
        Object k;
        if (e.hash == hash &&
            ((k = e.key) == key || (key != null && key.equals(k)))) {
            modCount++;
            size--;
            if (prev == e)  //删除的是头一个节点
                table[i] = next;
            else
                prev.next = next;
            e.recordRemoval(this);
            return e;
        }
        prev = e;
        e = next;
    }
    
    return e;
}
HashMap的使用场景

HashMap是Map接口下的集合,可以存放key - value键值对。当我们存储的元素需要有唯一标识,且对应一定的元素,可以使用HashMap,因为key为唯一的,且对应的value可以是一个元素,也可以是个对象。

HashMap的优势
查询效率时间复杂度近似于O(1)。

HashMap将查询效率放第一位,空间换时间。

HashMap可以自定义初始容量和加载因子,因此可以根据具体的使用场景,定义初始容量和加载因子。
HashMap的不足之处
  • 1、浪费空间
HashMap的初始容量为16,加载因子为0.75,当元素存储到12,需要进行扩容。

哈希冲突最大时:所有元素占用一个下标。

哈希冲突最低时:每一个元素都占一个下标。

数组容量为16时,数组的利用率为1 - 12。

所以HashMap采用空间换时间的方式,因为空间利用率越高,存储元素越多,哈希冲突就高,查询效率变低。
  • 2、依赖哈希算法
HashMap比较依赖哈希算法。

哈希算法设计的好,查询效率高。

哈希算法设计的不好,查询效率低。
  • 3、HashMap是无序的,插入结点是没有顺序的。
使用LinkedHashMap可以得到插入有序:

LinkedHashMap实现插入有序的原理:
	
	(1)LinkedHashMap在HashMap的基础上维护了一个双向链表。
		即:LinkedHashMap = HashMap + 双向链表
	
	(2)LinkedHashMap的节点构成:
		Entry<K,V> before, after,int hash, K key, V value, Entry<K,V> next
		
		相对于HashMap的节点多了after和before,Entry<K,V> before, after;
		
		before:指向该节点之前插入的节点;
		after:指向该节点之后插入的节点;
		
		继承了HashMap,添加等使用的都是HashMap,重写部分方法,维护before,after。
	
	(3)定义头指针和尾指针,维护双链表,采用头插和尾插添加元素。
  • 4、插入无序,无法按照key值得大小进行排序。
使用TreeMap可以维护key - value结构的大小顺序。

TreeMap:
	key-value的存储结构是根据key的大小比较排序得来的,而是不是通过key计算对应的哈希值。
	
	插入时,通过比较key插入到红黑树中,小的走左子树,大的走右子树。插入和删除都使用红黑树的规则。

底层结构:红黑树,将key-value作为一个节点存储在红黑树的节点中。

TreeMap需要使用比较器进行比较,给类提供比较原则。
HashMap的有关题型
  • 代码题:有一串任意长度英文字符串,统计这个字符串中每个字母出现的个数。
    • 要求:键盘输入:A~Z 任意字符;
    • 控制台输出:这个字符的个数;
    /**
     * 代码题:
     *      有一串任意长度英文字符串,统计这个字符串中每个字母出现的个数。
     *      要求:键盘输入:A~Z 任意字符       控制台输出:这个字符的个数
     */
    public static Map getCount(String str){
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0;i < str.length();i++){
            if(map.containsKey(str.charAt(i)))
                map.put(str.charAt(i),map.get(str.charAt(i)).intValue()+1);
            else
                map.put(str.charAt(i),1);
        }
        return map;
    }
    public static void main(String[] args) {
        System.out.println("请输入需要计算的字符串:");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        Map map = getCount(str);
        System.out.println(map);
    }

HashMap的常见面试题:https://blog.csdn.net/xintu1314/article/details/104825738

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值