java之jdk 1.8版本之前的HashMap

1. HashMap的数据结构

数据结构中有数组、链表来实现对数据的存储,接下来讲解HashMap

数组

数组存储区间是连续的,占用内存严重,故空间复杂的很大。但数组的二分查找时间复杂度小,为O(1);数组的特点是:寻址容易,插入和删除困难;
 

链表

链表存储区间离散,占用内存比较宽松,故空间复杂度很小,但时间复杂度很大,达O(N)。链表的特点是:寻址困难,插入和删除容易。

哈希表

那么我们能不能综合两者的特性,做出一种寻址容易,插入删除也容易的数据结构?答案是肯定的,这就是我们要提起的哈希表。哈希表((Hash table)既满足了数据的查找方便,同时不占用太多的内容空间,使用也十分方便。

  哈希表有多种不同的实现方法,我接下来解释的是最常用的一种方法—— 拉链法,我们可以理解为“链表的数组” ,如图:



从上图我们可以发现哈希表是由数组+链表组成的,一个长度为16的数组中,每个元素存储的是一个链表的头结点。那么这些元素是按照什么样的规则存储到数组中呢。一般情况是通过hash(key)%len获得,也就是元素的key的哈希值对数组长度取模得到。比如上述哈希表中,12%16=12,28%16=12,108%16=12,140%16=12。所以12、28、108以及140都存储在数组下标为12的位置,并且在同一个链表中按照插入数据排列。每次插入数据跟在前一个数据后面。
链表是有多个节点构成,每个节点称为Entry节点,包含四个属性分别是:Key(键对象)、value(值对象)、hash(键对象的hash值)、next(指向链表中下一个Entry对象引用地址)

重点:1、查找数据是首先会根据过hash(key)%len获得数据地址,在循环遍历整个链表获取数据

           2、有一个初始容量大小,默认是16,并且默认加载因子是0.75,当容量超过16x0.75=12时,就会触发扩容操作,就把数组的大小扩展为2*16=32,即扩大一倍,一旦触发扩容,已经存在的数据会重新计算存储位置,所以尽量避免扩容
           3、如果传key传空或者0,那么他们的hash值是0

 

2. HashMap的存取实现

既然是线性数组,为什么能随机存取?这里HashMap用了一个小算法,大致是这样实现:
// 存储时:
int hash = key.hashCode(); // 这个hashCode方法这里不详述,只要理解每个key的hash是一个固定的int值
int index = hash % Entry[].length;
Entry[index] = value;

// 取值时:
int hash = key.hashCode();
int index = hash % Entry[].length;
return Entry[index];

(1)、put 方法
 public V put(K key, V value) {

        if (key == null)

            return putForNullKey(value); //null总是放在数组的第一个链表中

        int hash = hash(key.hashCode());

        int i = indexFor(hash, table.length);

        //遍历链表

        for (Entry<K,V> e = table[i]; e != null; e = e.next) {

            Object k;

            //如果key在链表中已存在,则替换为新value

            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {

                V oldValue = e.value;

                e.value = value;

                e.recordAccess(this);

                return oldValue;

            }

        }

        modCount++;

        addEntry(hash, key, value, i);

        return null;

    }

 

void addEntry(int hash, K key, V value, int bucketIndex) {

    Entry<K,V> e = table[bucketIndex];

    table[bucketIndex] = new Entry<K,V>(hash, key, value, e); //参数e, 是Entry.next

    //如果size超过threshold,则扩充table大小。再散列

    if (size++ >= threshold)

            resize(2 * table.length);

}

(2)、get方法

 public V get(Object key) {

        if (key == null)

            return getForNullKey();

        int hash = hash(key.hashCode());

        //先定位到数组元素,再遍历该元素处的链表

        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.equals(k)))

                return e.value;

        }

        return null;

}


(3)、key为NULL的获取

null key总是存放在Entry[]数组的第一个元素。

   private V putForNullKey(V value) {

        for (Entry<K,V> e = table[0]; e != null; e = e.next) {

            if (e.key == null) {

                V oldValue = e.value;

                e.value = value;

                e.recordAccess(this);

                return oldValue;

            }

        }

        modCount++;

        addEntry(0, null, value, 0);

        return null;

    }

 

    private V getForNullKey() {

        for (Entry<K,V> e = table[0]; e != null; e = e.next) {

            if (e.key == null)

                return e.value;

        }

        return null;

    }