浅谈为什么hashmap效率高

基于hashmap的内部实现数据结构,可以理解为Entry(数组)+node(链表)

查询(读取)快:

public final int hashCode() {

    return Objects.hashCode(key) ^ Objects.hashCode(value);
}

根据hashcode(key)来锁定位置,然后获取value,相当于获取index下标,取得其value,类似于list集合中的实现集合类Arraylist实现方式。

插入快:

Node(int hash, K key, V value, Node<K,V> next) {

    this.hash = hash;
    this.key = key;
    this.value = value;
    this.next = next;
}

源码中参数中带有Node节点下个节点的引用,插入的时候只需要移动一下指针,指向下一个即可.与linkedlist有着异曲同工之妙.

总结:虽然比较来看hashmap效率与hashtable较高,但是多线程情况是不安全的。